|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
echoDreamz Smarty Rookie
Joined: 06 Jun 2008 Posts: 10
|
Posted: Sat Feb 21, 2009 4:35 am Post subject: Add items on the fly to the HTML <head> section |
|
|
Code: |
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File: function.head_item.php
* Type: function
* Name: head_item
* Purpose: assign items to global template <head> section
* -------------------------------------------------------------
*/
/**
* Smarty head_item function
*
* This function provides an easy way to add javascript files, css files
* and meta information into your templates <head> section
*
* @author Chris York (echoDreamz) <cyork@echodreamz.com>
* @copyright 2009 Chris York (echoDreamz)
*
* @param Array $params Array of parameters
* @param Smarty $smarty Smarty object reference
* @return String
*/
function smarty_function_head_item($params = array(), &$smarty)
{
// Sanitize check for required parameters
if (empty($params["type"]))
{
$smarty->trigger_error("head_item: Missing required parameter type. Available types: js, css, meta.", E_USER_ERROR);
}
if (!in_array($params["type"], array("js", "css", "meta")))
{
$smarty->trigger_error("head_item: Invalid type specified. Valid types: js, css, meta.", E_USER_ERROR);
}
if ($params["type"] == "js")
{
if (empty($params["src"]))
{
$smarty->trigger_error("head_item: Missing required parameter src.", E_USER_ERROR);
}
if (empty($params["files"]))
{
$smarty->trigger_error("head_item: Missing required parameter files.", E_USER_ERROR);
}
if (empty($params["language"]))
{
$params["language"] = "javascript";
}
if (empty($params["jsType"]))
{
$params["jsType"] = "text/javascript";
}
}
if ($params["type"] == "css")
{
if (empty($params["src"]))
{
$smarty->trigger_error("head_item: Missing required parameter src.", E_USER_ERROR);
}
if (empty($params["files"]))
{
$smarty->trigger_error("head_item: Missing required parameter files.", E_USER_ERROR);
}
if (empty($params["media"]))
{
$params["media"] = "screen";
}
}
if ($params["type"] == "meta")
{
if (empty($params["name"]))
{
$smarty->trigger_error("head_item: Missing required parameter name.", E_USER_ERROR);
}
if (empty($params["content"]))
{
$smarty->trigger_error("head_item: Missing required parameter content.", E_USER_ERROR);
}
}
// Final output
$output = "";
if ($params["type"] != "meta")
{
$files = explode(",", $params["files"]);
}
// Do the correct output
switch ($params["type"])
{
// javascript
case "js":
foreach ($files as $file)
{
$output .= "<smarty_head_item><script language=\"{$params['language']}\" type=\"{$params['jsType']}\" src=\"{$params['src']}/{$file}\"></script></smarty_head_item>\n";
}
break;
// CSS
case "css":
foreach ($files as $file)
{
$output .= "<smarty_head_item><link type=\"text/css\" media=\"{$params['media']}\" href=\"{$params['src']}/{$file}\" /></smarty_head_item>\n";
}
break;
// Meta
case "meta":
$output = "<smarty_head_item><meta name=\"{$params['name']}\" content=\"{$params['content']}\" /></smarty_head_item>\n";
break;
}
return $output;
}
?>
|
Code: |
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File: outputfilter.head_item_add.php
* Type: outputfilter
* Name: head_item_add
* Purpose: assign items to global template <head> section
* -------------------------------------------------------------
*/
/**
* Smarty head_item function
*
* This function provides an easy way to add javascript files, css files
* and meta information into your templates <head> section
*
* @author Chris York (echoDreamz) <cyork@echodreamz.com>
* @copyright 2009 Chris York (echoDreamz)
*
* @param String $tpl_output Template output
* @param Smarty $smarty Smarty object reference
*/
function smarty_outputfilter_head_item_add($tpl_output, &$smarty)
{
// Regex matches
$matches = array();
// Find all <smarty_head_item> sections
preg_match_all("{<smarty_head_item>(.*?)</smarty_head_item>}", $tpl_output, $matches);
// Remove them from the tpl source
$tpl_output = preg_replace("{<smarty_head_item>(.*?)</smarty_head_item>\n}", "", $tpl_output);
// Create the final template output
return str_replace("</head>", implode("\n", array_unique($matches[1])) . "\n</head>", $tpl_output);
}
?>
|
Usage:
Code: | $smarty->load_filter("output", "head_item_add"); |
Template
Code: |
<{head_item type="css" src="http://www.yoursite.com/styles" files="test.css,test2.css"}>
<{head_item type="js" src="http://www.yoursite.com/js" files="test.js,mootools.js,another.js"}>
|
Hopefully someone finds it useful! |
|
Back to top |
|
ernest Smarty n00b
Joined: 02 Mar 2009 Posts: 1
|
Posted: Mon Mar 02, 2009 7:02 pm Post subject: |
|
|
Thank you very much for the function/filter. This is excatly what i was looking for.
It works perfect after a few tweaks. The following are the problems i encountered / fixed for my smarty (hope it helps someone):
1. in my "<head>" section, i have "<base href="........" />", so the javascript/css src links do not require a "src" parameter in the calling function and i have to comment out the requirement in function.head_item.php. Should make it optional!
2. in the javascript output, it has "language=...." which is not XHTML valid according to W3C (which i deleted)
3. since i already have a default js file in the head section, inserting the "dynamic" javascript file just before the </head> tag caused problems for me (i am using prototype & scriptaculous for a few pages only). So i think is is better to insert a tag like "<insert_head_item>" into the <head> section and then replace it with the custom/dynamic code. Gives us more control as to where to place the files. (also need to change str_replace function in outputfilter.head_item_add.php)
4. i am pretty new to smarty, but i am wondering: could use it as a postfilter instead of output filter? According to my understanding, output filter is performed on every request (even my pages are already cached), but postfilter is processed before it is saved into cache (therefore improves performance)??
Ernest |
|
Back to top |
|
Quleczka Smarty n00b
Joined: 09 Mar 2009 Posts: 3
|
Posted: Mon Mar 09, 2009 3:28 pm Post subject: |
|
|
nice idea
ernest wrote: |
2. in the javascript output, it has "language=...." which is not XHTML valid according to W3C (which i deleted)
|
I agree I modified it as well. I also added rel="stylesheet" for stylesheets cause it is not working without it.
Only thing I don't understand is why you need this second outputfilter file and the whole idea of adding <smarty_head_item> tag which is removed later?
It works perfectly only with function.head_item.php. You just have to remove <smarty_head_tag> everywhere while assigning $output and call it as {head_item type="css" src="." files="style.css"} without <> arround.
And even don't have to call load_filter().
By the way adding support for base tag whould be nice as well |
|
Back to top |
|
Quleczka Smarty n00b
Joined: 09 Mar 2009 Posts: 3
|
Posted: Mon Mar 09, 2009 4:52 pm Post subject: |
|
|
I also added removing one new line at the end And removed "/" so source is now specified like usually "./" or "./js/" or "./css/" instead of "./js" as it was necessary earlier
My code is now like this
Code: | <?php
function smarty_function_head_item($params = array(), &$smarty)
{
// Sanitize check for required parameters
if (empty($params["type"]))
{
$smarty->trigger_error("head_item: Missing required parameter type. Available types: js, css, meta.", E_USER_ERROR);
}
if (!in_array($params["type"], array("js", "css", "meta")))
{
$smarty->trigger_error("head_item: Invalid type specified. Valid types: js, css, meta.", E_USER_ERROR);
}
if ($params["type"] == "js")
{
if (empty($params["src"]))
{
$smarty->trigger_error("head_item: Missing required parameter src.", E_USER_ERROR);
}
if (empty($params["files"]))
{
$smarty->trigger_error("head_item: Missing required parameter files.", E_USER_ERROR);
}
if (empty($params["jsType"]))
{
$params["jsType"] = "text/javascript";
}
}
if ($params["type"] == "css")
{
if (empty($params["src"]))
{
$smarty->trigger_error("head_item: Missing required parameter src.", E_USER_ERROR);
}
if (empty($params["files"]))
{
$smarty->trigger_error("head_item: Missing required parameter files.", E_USER_ERROR);
}
if (empty($params["media"]))
{
$params["media"] = "screen";
}
}
if ($params["type"] == "meta")
{
if (empty($params["name"]))
{
$smarty->trigger_error("head_item: Missing required parameter name.", E_USER_ERROR);
}
if (empty($params["content"]))
{
$smarty->trigger_error("head_item: Missing required parameter content.", E_USER_ERROR);
}
}
// Final output
$output = "";
if ($params["type"] != "meta")
{
$files = explode(",", $params["files"]);
}
// Do the correct output
switch ($params["type"])
{
// javascript
case "js":
foreach ($files as $file)
{
$output .= "<script type=\"{$params['jsType']}\" src=\"{$params['src']}{$file}\"></script>\n";
}
break;
// CSS
case "css":
foreach ($files as $file)
{
$output .= "<link rel='stylesheet' type=\"text/css\" media=\"{$params['media']}\" href=\"{$params['src']}{$file}\" />\n";
}
break;
// Meta
case "meta":
$output = "<meta name=\"{$params['name']}\" content=\"{$params['content']}\" />\n";
break;
}
//removing extra space at the end
return substr($output,0,-1);
}
?> |
and I use it for example like this
Code: |
{head_item type="meta" name="content-type" content="text/html; charset=iso-8859-1"}
{head_item type="css" src="./" files="style.php"}
{head_item type="js" src="./js/" files="livevalidation.js"}
|
no extra <> around smarty tag and all in one function - works perfect for me
p.s. I removed header just because system didn't like links inside it |
|
Back to top |
|
Quleczka Smarty n00b
Joined: 09 Mar 2009 Posts: 3
|
Posted: Mon Mar 09, 2009 5:28 pm Post subject: |
|
|
version with added extra trimming cause earlier even one space after coma was causing incorrect result:
Code: | <?php
function smarty_function_head_item($params = array(), &$smarty)
{
// Sanitize check for required parameters
if (empty($params["type"]))
{
$smarty->trigger_error("head_item: Missing required parameter type. Available types: js, css, meta.", E_USER_ERROR);
}
if (!in_array($params["type"], array("js", "css", "meta")))
{
$smarty->trigger_error("head_item: Invalid type specified. Valid types: js, css, meta.", E_USER_ERROR);
}
if ($params["type"] == "js")
{
if (empty($params["src"]))
{
$smarty->trigger_error("head_item: Missing required parameter src.", E_USER_ERROR);
}
if (empty($params["files"]))
{
$smarty->trigger_error("head_item: Missing required parameter files.", E_USER_ERROR);
}
if (empty($params["jsType"]))
{
$params["jsType"] = "text/javascript";
}
}
if ($params["type"] == "css")
{
if (empty($params["src"]))
{
$smarty->trigger_error("head_item: Missing required parameter src.", E_USER_ERROR);
}
if (empty($params["files"]))
{
$smarty->trigger_error("head_item: Missing required parameter files.", E_USER_ERROR);
}
if (empty($params["media"]))
{
$params["media"] = "screen";
}
}
if ($params["type"] == "meta")
{
if (empty($params["name"]))
{
$smarty->trigger_error("head_item: Missing required parameter name.", E_USER_ERROR);
}
if (empty($params["content"]))
{
$smarty->trigger_error("head_item: Missing required parameter content.", E_USER_ERROR);
}
}
// Final output
$output = "";
if ($params["type"] != "meta")
{
$files = explode(",", $params["files"]);
}
// Do the correct output
switch ($params["type"])
{
// javascript
case "js":
foreach ($files as $file)
{
$output .= "<script type=\"{$params['jsType']}\" src=\"".trim($params['src']).trim($file)."\"></script>\n";
}
break;
// CSS
case "css":
foreach ($files as $file)
{
$output .= "<link rel='stylesheet' type=\"text/css\" media=\"{$params['media']}\" href=\"".trim($params['src']).trim($file)."\" />\n";
}
break;
// Meta
case "meta":
$output = "<meta name=\"{$params['name']}\" content=\"{$params['content']}\" />\n";
break;
}
//removing extra space at the end
return substr($output,0,-1);
}
?> |
|
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|