Smarty : {capture} to put code in your header page

If you are using Smarty to generate your templates, I strongly advice you to take advantage of the function: {capture}

This function is used to collect the output of the template between the tags {capture}{/capture} into a variable instead of displaying it.

Exemple  :

index.tpl

{capture name=header}
{literal}
<script type="text/javascript">
alert('Javascript in head');
</script>
{/literal}
{/capture}
<!-- Include Page Header using Smarty {include} -->
{include file="header.tpl"}
My index page content

header.tpl

<html>
<head>
<title>Page ttle</title>
{$smarty.capture.header}
</head>

Nb : “Javascript code” must be placed between {literal}{/literal}

Comments (1)

LoganJune 29th, 2010 at 5:55 pm

Literal is not required to wrap around javascript unless there are { and } in it and you have those marked as your delimeters in smarty. Also then you can still use ldelim and rdelim.

Leave a comment

Your comment