Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

186 lines (164 sloc) 9.574 kb
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Quick introduction to Xslate</title>
<link rel="stylesheet" type="text/css" href="css/reset.css" />
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<link rel="stylesheet" type="text/css" href="css/perl-highlight.css" />
</head>
<body>
<div class="wrapper">
<div class="header">
<h1>Quick Introduction to Xslate</h1>
<div class="description">
How to use Xslate
</div>
</div>
<div id="content">
<div class="section">
<h2>Overview</h2>
<p>We will introduce you to the most simple usage of <a href="http://xslate.org" />Xslate</a>.</p>
<p>Main subjects of this page are listed below:</p>
<ul>
<li>Iteration</li>
<li>The dump filter - useful for debugging</li>
<li>mark_raw() - To output a text without escaping</li>
</ul>
</div>
<div class="section clearfix">
<h2>Example</h2>
<ul>
<li><code>$books</code> (hashref) contains information of Perl books.</li>
<li><code>$html</code> contains a HTML source string. You must use <code>mark_raw()</code> in applications in order to interpolate HTML sources into templates. Otherwise meta characters in template variables will be escaped automatically.</li>
</ul>
<h3>Script: perlbook.pl</h3>
<pre><code><span class="comment">#!/usr/bin/env perl
</span><span class="keyword">use</span> <span class="pragma">strict</span><span class="structure">;</span>
<span class="keyword">use</span> <span class="pragma">warnings</span><span class="structure">;</span>
<span class="keyword">use</span> <span class="pragma">utf8</span><span class="structure">;</span>
<span class="keyword">use</span> <span class="word">Encode</span> <span class="words">qw(encode_utf8)</span><span class="structure">;</span>
<span class="keyword">use</span> <span class="word">FindBin</span> <span class="words">qw($Bin)</span><span class="structure">;</span>
<span class="keyword">use</span> <span class="word">Text::Markdown</span> <span class="words">qw(markdown)</span><span class="structure">;</span>
<span class="keyword">use</span> <span class="word">Text::Xslate</span> <span class="words">qw(mark_raw)</span><span class="structure">;</span>
<span class="keyword">my</span> <span class="symbol">$xslate</span> <span class="operator">=</span> <span class="word">Text::Xslate</span><span class="operator">-&gt;</span><span class="word">new</span><span class="structure">(</span>
<span class="word">path</span> <span class="operator">=&gt;</span> <span class="structure">[</span><span class="double">&quot;$Bin/templates&quot;</span><span class="structure">]</span><span class="operator">,</span>
<span class="structure">);</span>
<span class="keyword">my</span> <span class="symbol">$books</span> <span class="operator">=</span> <span class="structure">{</span>
<span class="double">&quot;978-0596000271&quot;</span> <span class="operator">=&gt;</span> <span class="structure">{</span>
<span class="word">name</span> <span class="operator">=&gt;</span> <span class="double">&quot;Programming Perl&quot;</span><span class="operator">,</span>
<span class="word">pages</span> <span class="operator">=&gt;</span> <span class="number">1092</span><span class="operator">,</span>
<span class="structure">}</span><span class="operator">,</span>
<span class="comment"> # Programming Perl in Russian
</span> <span class="double">&quot;978-5932860205&quot;</span> <span class="operator">=&gt;</span> <span class="structure">{</span>
<span class="word">name</span> <span class="operator">=&gt;</span> <span class="double">&quot;Программирование на Perl&quot;</span><span class="operator">,</span>
<span class="word">pages</span> <span class="operator">=&gt;</span> <span class="number">1152</span><span class="operator">,</span>
<span class="structure">}</span><span class="operator">,</span>
<span class="comment"> # Programming Perl in Japanese
</span> <span class="double">&quot;978-4873110967&quot;</span> <span class="operator">=&gt;</span> <span class="structure">{</span>
<span class="word">name</span> <span class="operator">=&gt;</span> <span class="double">&quot;プログラミングPerl Vol.1&quot;</span><span class="operator">,</span>
<span class="word">pages</span> <span class="operator">=&gt;</span> <span class="number">708</span><span class="operator">,</span>
<span class="structure">}</span><span class="operator">,</span>
<span class="double">&quot;978-4873110974&quot;</span> <span class="operator">=&gt;</span> <span class="structure">{</span>
<span class="word">name</span> <span class="operator">=&gt;</span> <span class="double">&quot;プログラミングPerl Vol.2&quot;</span><span class="operator">,</span>
<span class="word">pages</span> <span class="operator">=&gt;</span> <span class="number">1303</span><span class="operator">,</span>
<span class="structure">}</span><span class="operator">,</span>
<span class="structure">};</span>
<span class="keyword">my</span> <span class="symbol">$see_also</span> <span class="operator">=</span> <span class="word">markdown</span><span class="structure">(</span><span class="heredoc">&lt;&lt;'END_MD'</span><span class="structure">);</span> <span class="comment"># an HTML generator</span>
<span class="heredoc_content">SEE ALSO
-----------------
* http://perl.com/
* http://perl.org/
* http://search.cpan.org/
</span><span class="heredoc_terminator">END_MD
</span>
<span class="keyword">my</span> <span class="symbol">$content</span> <span class="operator">=</span> <span class="symbol">$xslate</span><span class="operator">-&gt;</span><span class="word">render</span><span class="structure">(</span><span class="double">&quot;perlbook.tx&quot;</span><span class="operator">,</span> <span class="structure">{</span>
<span class="word">title</span> <span class="operator">=&gt;</span> <span class="double">&quot;Perl Books&quot;</span><span class="operator">,</span>
<span class="word">books</span> <span class="operator">=&gt;</span> <span class="symbol">$books</span><span class="operator">,</span>
<span class="word">see_also</span> <span class="operator">=&gt;</span> <span class="word">mark_raw</span><span class="structure">(</span><span class="symbol">$see_also</span><span class="structure">)</span><span class="operator">,</span>
<span class="structure">});</span>
<span class="word">print</span> <span class="word">encode_utf8</span><span class="structure">(</span><span class="symbol">$content</span><span class="structure">);</span>
<span class="separator">__END__</span>
</code></pre>
<h3>Template: templates/perlbook.tx</h3>
<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot; /&gt;
&lt;title&gt;&lt;: $title :&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;&lt;: $title :&gt;&lt;/h1&gt;
&lt;ol&gt;
: for $books.keys() -&gt; $isbn {
&lt;li&gt;&lt;: $books[$isbn].name :&gt; - &lt;: $books[$isbn].pages :&gt; pages / ISBN-13 : &lt;: $isbn :&gt;&lt;/li&gt;
: }
&lt;/ol&gt;
&lt;pre&gt;
$books =
: $books | dump
&lt;/pre&gt;
: $see_also # interpolate HTML components
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<h3>Output:</h3>
<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot; /&gt;
&lt;title&gt;Perl Books&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Perl Books&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;Programming Perl - 1092 pages / ISBN-13 : 978-0596000271&lt;/li&gt;
&lt;li&gt;プログラミングPerl Vol.1 - 708 pages / ISBN-13 : 978-4873110967&lt;/li&gt;
&lt;li&gt;プログラミングPerl Vol.2 - 1303 pages / ISBN-13 : 978-4873110974&lt;/li&gt;
&lt;li&gt;Программирование на Perl - 1152 pages / ISBN-13 : 978-5932860205&lt;/li&gt;
&lt;/ol&gt;
&lt;pre&gt;
$books =
(
&amp;apos;978-0596000271&amp;apos; =&amp;gt; {
name =&amp;gt; &amp;apos;Programming Perl&amp;apos;,
pages =&amp;gt; 1092
},
&amp;apos;978-4873110967&amp;apos; =&amp;gt; {
name =&amp;gt; &amp;apos;プログラミングPerl Vol.1&amp;apos;,
pages =&amp;gt; 708
},
&amp;apos;978-4873110974&amp;apos; =&amp;gt; {
name =&amp;gt; &amp;apos;プログラミングPerl Vol.2&amp;apos;,
pages =&amp;gt; 1303
},
&amp;apos;978-5932860205&amp;apos; =&amp;gt; {
name =&amp;gt; &amp;apos;Программирование на Perl&amp;apos;,
pages =&amp;gt; 1152
}
)
&lt;/pre&gt;
&lt;h2&gt;SEE ALSO&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;http://perl.com/&lt;/li&gt;
&lt;li&gt;http://perl.org/&lt;/li&gt;
&lt;li&gt;http://search.cpan.org/&lt;/li&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<div class="section clearfix">
<h2>SEE ALSO</h2>
<ul>
<li><a href="http://search.cpan.org/dist/Text-Xslate/">Documents on CPAN</a></li>
<li><a href="http://search.cpan.org/perldoc?Text::Xslate::Manual::FAQ">FAQ</a></li>
<li><a href="http://search.cpan.org/perldoc?Text::Xslate::Manual::Cookbook">Cookbook</a></li>
</ul>
</div>
</div>
<div class="footer">
If you have requests for this web site, please send pull request on <a href="http://github.com/xslate/xslate.github.com">github</a>.
</div>
</div>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.