Files
smarty/docs/en/designers/language-basic-syntax.xml
2004-03-28 16:48:54 +00:00

249 lines
7.3 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
<chapter id="language.basic.syntax">
<title>Basic Syntax</title>
<para>
All Smarty template tags are enclosed within delimiters. By
default, these delimiters are <literal>{</literal> and
<literal>}</literal>, but they can be changed.
</para>
<para>
For these examples, we will assume that you are using the default
delimiters. In Smarty, all content outside of delimiters is displayed as
static content, or unchanged. When Smarty encounters template tags, it
attempts to interpret them, and displays the appropriate output in their
place.
</para>
<sect1 id="language.syntax.comments">
<title>Comments</title>
<para>
Template comments are surrounded by asterisks, and that is surrounded
by the delimiter tags like so: {* this is a comment *}
Smarty comments are not displayed in the final output of the template.
They are used for making internal notes in the templates.
</para>
<example>
<title>Comments</title>
<programlisting>
<![CDATA[
{* Smarty *}
{* include the header file here *}
{include file="header.tpl"}
{include file=$includeFile}
{include file=#includeFile#}
{* display dropdown lists *}
<select name="company">
{html_options values=$vals selected=$selected output=$output}
</select>
]]>
</programlisting>
</example>
</sect1>
<sect1 id="language.syntax.functions">
<title>Functions</title>
<para>
Each Smarty tag either prints a
<link linkend="language.variables">variable</link> or invokes some sort
of function. Functions are processed and displayed by enclosing the
function and its attributes into delimiters like so: {funcname
attr1="val" attr2="val"}.
</para>
<example>
<title>function syntax</title>
<programlisting>
<![CDATA[
{config_load file="colors.conf"}
{include file="header.tpl"}
{if $highlight_name}
Welcome, <font color="{#fontColor#}">{$name}!</font>
{else}
Welcome, {$name}!
{/if}
{include file="footer.tpl"}
]]>
</programlisting>
</example>
<para>
Both built-in functions and custom functions have the same syntax in
the templates. Built-in functions are the inner workings of Smarty,
such as <command>if</command>, <command>section</command> and
<command>strip</command>. They cannot be modified. Custom functions are
additional functions implemented via plugins. They can be modified to
your liking, or you can add new ones. <command>html_options</command> and
<command>html_select_date</command> are examples of custom functions.
</para>
</sect1>
<sect1 id="language.syntax.attributes">
<title>Attributes</title>
<para>
Most of the functions take attributes that specify or modify
their behavior. Attributes to Smarty functions are much like HTML
attributes. Static values don't have to be enclosed in quotes, but it
is recommended for literal strings. Variables may also be used, and
should not be in quotes.
</para>
<para>
Some attributes require boolean values (true or false). These can be
specified as either unquoted <literal>true</literal>,
<literal>on</literal>, and <literal>yes</literal>, or
<literal>false</literal>, <literal>off</literal>, and
<literal>no</literal>.
</para>
<example>
<title>function attribute syntax</title>
<programlisting>
<![CDATA[
{include file="header.tpl"}
{include file=$includeFile}
{include file=#includeFile#}
{html_select_date display_days=yes}
<select name="company">
{html_options values=$vals selected=$selected output=$output}
</select>
]]>
</programlisting>
</example>
</sect1>
<sect1 id="language.syntax.quotes">
<title>Embedding Vars in Double Quotes</title>
<para>
Smarty will recognize assigned variables embedded in double quotes so long
as the variables contain only numbers, letters, underscores and brackets
[]. With any other characters (period, object reference, etc.) the variable
must be surrounded by backticks.
</para>
<example>
<title>embedded quotes syntax</title>
<programlisting>
<![CDATA[
SYNTAX EXAMPLES:
{func var="test $foo test"} <-- sees $foo
{func var="test $foo_bar test"} <-- sees $foo_bar
{func var="test $foo[0] test"} <-- sees $foo[0]
{func var="test $foo[bar] test"} <-- sees $foo[bar]
{func var="test $foo.bar test"} <-- sees $foo (not $foo.bar)
{func var="test `$foo.bar` test"} <-- sees $foo.bar
PRACTICAL EXAMPLES:
{include file="subdir/$tpl_name.tpl"} <-- will replace $tpl_name with value
{cycle values="one,two,`$smarty.config.myval`"} <-- must have backticks
]]>
</programlisting>
</example>
</sect1>
<sect1 id="language.math">
<title>Math</title>
<para>
Math can be applied directly to variable values.
</para>
<example>
<title>math examples</title>
<programlisting>
<![CDATA[
{$foo+1}
{$foo*$bar}
{* some more complicated examples *}
{$foo->bar-$bar[1]*$baz->foo->bar()-3*7}
{if ($foo+$bar.test%$baz*134232+10+$b+10)}
{$foo|truncate:"`$fooTruncCount/$barTruncFactor-1`"}
{assign var="foo" value="`$foo+$bar`"}
]]>
</programlisting>
</example>
</sect1>
<sect1 id="language.escaping">
<title>Escaping Smarty Parsing</title>
<para>
It is sometimes desirable or even necessary to have Smarty ignore sections it
would otherwise parse. A classic example is embedding Javascript or CSS code in
a template. The problem arises as those languages use the { and } characters
which are also the default delimiters for Smarty.
</para>
<para>
The simplest thing is to avoid the situation altogether by separating your Javascript
and CSS code into their own files and then using standard HTML methods to access them.
</para>
<para>
Including literal content is possible using <link
linkend="language.function.literal">{literal} .. {/literal}</link> blocks.
Similar to HTML entity usage, you can use <link
linkend="language.function.ldelim">{ldelim}</link> and <link
linkend="language.function.ldelim">{rdelim}</link> to display the current delimiters.
</para>
<para>
It is often convenient to simply change Smarty's <link
linkend="variable.left.delimiter">$left_delimiter</link> and
<link linkend="variable.right.delimiter">$right_delimiter</link>.
</para>
<example>
<title>changing delimiters example</title>
<programlisting role="php">
<![CDATA[
<?php
$smarty = new Smarty;
$smarty->left_delimiter = '<!--{';
$smarty->right_delimiter = '}-->';
$smarty->assign('foo', 'bar');
$smarty->display('example.tpl');
?>
--- example.tpl
<script language="javascript">
var foo = <!--{$foo}-->;
function dosomething() {
alert("foo is " + foo);
}
dosomething();
</script>
]]>
</programlisting>
</example>
</sect1>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->