I am building a website using PHP + Slim framework + Twig as templating engine combination.
I have a default.php page with a common page layout inherited in all the other pages in the website. My intention is to specify different CSS or Javascripts in each of the twig template files that inherit the default.php page so that I can have the flexibility in functionality and appeal. Looks like ASSETIC is the only best asset management tool for Slim framework based on my google searches so far. Though everyone is suggesting to use ASSETIC, there is no complete documentation on how to set it up and use in SLIM framework. I have been struggling with this for almost a week.
My folder structure is as shown below:
I am using the below code now for ASSETIC setup.
//Filter manager
$fm = new FilterManager();
$fm->set( 'cssmin' , new CSSMinFilter());
$fm->set( 'cssmin' , new JSMinFilter());
$fm->set('yui_css', new Yui\CssCompressorFilter('path/to/yui
compressro.jar'));
$factory = new AssetFactory('C:/xampp/htdocs/OLD/public');
$factory->setDebug(true);
$factory->setFilterManager($fm);
//$factory->addWorker(new
//CacheBustingWorker(CacheBustingWorker::STRATEGY_CONTENT));
$twigTemplateDirs = array(INC_ROOT.'/app/views/');
$twigOptions = array(
'charset' => 'utf-8',
//'cache' => realpath(CACHE_ROOT.'/app/views/'),
'auto_reload' => true,
'strict_variables' => false,
'autoescape' => true
);
$twigExtensions = array('Twig_Extensions_Slim', new
AsseticExtension($factory));
//Twig view engine - twig is already added to the slim container by name
//view
//$app->view
// Set LazyAssetManager, passing in the current AssetFoctory object and
//Twig's data
$asset_manger = new LazyAssetManager($factory);
$asset_manger->setLoader( 'twig' , new TwigFormulaLoader($app->view-
>getEnvironment()));
//Read all templates into the data manager, waiting to write to the file
$templates = array ();
$directories = array (INC_ROOT.'/app/views/');
while (sizeof($directories)) {
$directory = array_pop($directories);
foreach(glob($directory."/*") as $file_path) {
if (is_dir($file_path) === true) {
array_push($directories, $file_path);
}elseif (is_file($file_path) === true && preg_match("/\.(html)$/",
$file_path) == true) {
$templates[] = str_replace(INC_ROOT.'/app/views/', '', $file_path);
}
}
}
foreach ($templates as $template) {
$resource = new TwigResource(new
\Twig_Loader_Filesystem(INC_ROOT.'/app/views/'), $template);
$asset_manger->addResource($resource, 'twig');
}
// Write the currently interpreted Asset tag to the specified file
$asset_writer = new AssetWriter(INC_ROOT);
$asset_writer->writeManagerAssets($asset_manger);
My questions:
What do I specify in the default.php file for using css and javascript code?
{% block block_name %}{% endblock %}
The documentation says, we can refer the css and javascript files in the twig template file as shown below but I get a unknown stylesheet tag error:
{% stylesheets 'public\css\test.css' filter='yui_css' output='css/all.css' %} {% endstylesheets %}
I am not very sure about my PUBLIC_ROOT, VIEWS_ROOT and WWW_ROOT used in the above code. I am using PUBLIC_ROOT as "C:\xampp\htdocs\OLD" where all project related files reside, VIEWS_ROOT as "C:\xampp\htdocs\OLD\app\views" where all my views and templates reside and WWW_ROOT same as PUBLIC_ROOT. Please let me know if this is correct.
Is the above code correct or am I missing anything?
I am doing trial and error with all the above as I could not find a complete documentation explaining the functionality of the assetic library.