This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

TWIG Problem

This forum is for non code-based questions, best practises, IDEs, version control tools, work opportunities, etc.

TWIG Problem

by Bogey » Mon Feb 10, 2014 12:53 am

I recently set-up TWIG as a template engine for a local project of mine, and I also wanted to run css files through TWIG for parsing, so this is how I went about it.

I set up .htaccess url rewrite and put the following code in there.

Code: Select all
<IfModule mod_rewrite.c>
    RewriteEngine On
   
    RewriteRule (.*/.*\.css)$ ./parse_file.php?file=$1 [NC]
</IfModule>


And in parse_file.php I mainly have the following code

Code: Select all
<?php
/*
 * index.php
 *      The main page of the system
**/

// Requiring TWIG (Template Engine) http://twig.sensiolabs.org/
include_once('twig/Autoloader.php');

// Including our definitions
include_once('includes/definitions.php');

// Including the error management system
include_once('includes/error_handling.php');

// Initiating the error object here as it is required in user.php
$error = new error_handling();

// Including the user management system
include_once('includes/user.php');

// Initiating the required classes
$user = new user();
Twig_Autoloader::register();

// Setting the default location of the template directories
$tpl_loc = './templates/' . $user->style;

// Retrieving the file we are supposed to be parsing (css mainly)
$file = (isset($_GET['file'])) ? $_GET['file'] : null;

// Validating the file
if(!is_file($tpl_loc . '/styles/' . $file))
{
    // Making sure we are not gracing the errors
    if(GRACEFUL_ERRORS)
    {
        $error->error($user->lang('404'), $user->lang('FILE_NOT_FOUND'), ERROR_FATAL);
    }
}

// Setting the TWIG loader resource
$loader = new Twig_Loader_Filesystem($tpl_loc . '/styles/');

// Setting the TWIG environment
$twig = new Twig_Environment($loader);

// The replacement array
$replacements  =  array('CSS_URL' => $tpl_loc . '/styles/',
                                    'IMG_URL' => $tpl_loc . '/images/'
                                   );

// Rendering the file
$twig->render($file, $replacements);
?>


It seems to work, it redirects the CSS to parse_file.php, but the CSS doesn't get parsed through TWIG and doesn't get returned. When I view the generated source code and click on a stylesheet, I see the following error:

<br />
<b>Fatal error</b>: Uncaught exception 'Twig_Error_Loader' with message 'Unable to find template &quot;templates/default/styles/menu.css&quot; (looked into: ./templates/default/styles).' in C:\xampp\htdocs\www\cms\twig\Loader\Filesystem.php:202
Stack trace:
#0 C:\xampp\htdocs\www\cms\twig\Loader\Filesystem.php(138): Twig_Loader_Filesystem-&gt;findTemplate('templates/defau...')
#1 C:\xampp\htdocs\www\cms\twig\Environment.php(265): Twig_Loader_Filesystem-&gt;getCacheKey('templates/defau...')
#2 C:\xampp\htdocs\www\cms\twig\Environment.php(323): Twig_Environment-&gt;getTemplateClass('templates/defau...', NULL)
#3 C:\xampp\htdocs\www\cms\twig\Environment.php(292): Twig_Environment-&gt;loadTemplate('templates/defau...')
#4 C:\xampp\htdocs\www\cms\parse_file.php(57): Twig_Environment-&gt;render('templates/defau...', Array)
#5 {main}
thrown in <b>C:\xampp\htdocs\www\cms\twig\Loader\Filesystem.php</b> on line <b>202</b><br />


The thing is: the file path where TWIG is looking for the files are correct, the stylesheet IS there but TWIG is having problems finding it for some reason... and the stack trace seems to show that it is using the correct parameters, but can't tell for sure since it has being cut off.

Any help? I guess I could write a special templating class simply for stylesheets but before I do that I'm going to try and get it to work with TWIG. Reason I need this (right now the project is fresh) is that I'm going to have some values being retrieved from the database and used in CSS.

I don't want to insert the stylesheets into the document hard-coded because I have three different stylesheets (separated for organizational purposes) and combining them would defeat the organization and make each file really big.

Thank you!
Bogey
Junior Member
 
Posts: 3
Joined: Mon Feb 10, 2014 12:33 am

Re: TWIG Problem

by Bogey » Fri Feb 21, 2014 2:12 am

Any help on this? I have not being able to find a solution for this yet.
Bogey
Junior Member
 
Posts: 3
Joined: Mon Feb 10, 2014 12:33 am

Re: TWIG Problem

by nymo » Fri Feb 21, 2014 2:05 pm

I can't help you directly with your problem but why not including all three stylesheets into your twig file and then just include the one you need on each page and disable the others via template inheritance or usage of blocks?

As a second thought I would suggest you compress/minify your stylesheets with YUI or something similar.
nymo
Senior Member
 
Posts: 174
Joined: Fri Jan 07, 2011 11:37 pm
Location: Germany

Re: TWIG Problem

by Bogey » Tue Feb 25, 2014 2:48 am

I use all three for pretty much all of my pages... one defines the layout, the other colorizes it, the other is for the menus another would be for forms... just organization kind of thing.

Never heard of YUI though... what is that?
Bogey
Junior Member
 
Posts: 3
Joined: Mon Feb 10, 2014 12:33 am

Re: TWIG Problem

by nymo » Tue Feb 25, 2014 1:12 pm

nymo
Senior Member
 
Posts: 174
Joined: Fri Jan 07, 2011 11:37 pm
Location: Germany