<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Nette Framework forum : Using Nette Framework : Beginners : Pass Variable to template</title>
<link>https://forum.nette.org/en</link>
<description>Nette Framework forum</description>
<language>en</language>
<docs>http://backend.userland.com/rss</docs>
<item>
	<title>Pass Variable to template (Beginners)</title>
	<dc:creator>madcoda9000</dc:creator>
	<link>https://forum.nette.org/en/36840#p228602</link>
	<guid isPermaLink="false">228602@http://forum.nette.org/en</guid>
	<description>&lt;p&gt;&lt;span class=&quot;mention&quot;&gt;@DavidGrudl&lt;/span&gt; Awesome! That is exactly what i was
looking for. Many thanks.&lt;/p&gt;

&lt;p&gt;Btw: is there a similiar way to pass a global constant to all templates? For
ex. a language variable?&lt;/p&gt;
</description>
	<pubDate>Sat, 26 Apr 2025 06:43:36 +0200</pubDate>
</item>
<item>
	<title>Pass Variable to template (Beginners)</title>
	<dc:creator>David Grudl</dc:creator>
	<link>https://forum.nette.org/en/36840#p228600</link>
	<guid isPermaLink="false">228600@http://forum.nette.org/en</guid>
	<description>&lt;p&gt;I think you&apos;re looking for addFunction&lt;/p&gt;

&lt;pre
class=&quot;language-php&quot;&gt;&lt;code&gt;$latte-&amp;gt;addFunction(&apos;trans&apos;, fn($s) =&amp;gt; $app-&amp;gt;get(&apos;trans&apos;)($s));&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and usage&lt;/p&gt;

&lt;pre class=&quot;language-latte&quot;&gt;&lt;code&gt;{trans(&apos;login.username&apos;)}&lt;/code&gt;&lt;/pre&gt;
</description>
	<pubDate>Fri, 25 Apr 2025 11:43:41 +0200</pubDate>
</item>
<item>
	<title>Pass Variable to template (Beginners)</title>
	<dc:creator>madcoda9000</dc:creator>
	<link>https://forum.nette.org/en/36840#p228599</link>
	<guid isPermaLink="false">228599@http://forum.nette.org/en</guid>
	<description>&lt;p&gt;Thanks for your answer. That means I&apos;ve to create an extension to use an
translation variable in an latte template. Pfuhhh, that sounds complicated to
me.. :-(&lt;/p&gt;
</description>
	<pubDate>Fri, 25 Apr 2025 10:47:04 +0200</pubDate>
</item>
<item>
	<title>Pass Variable to template (Beginners)</title>
	<dc:creator>Marek Bartoš</dc:creator>
	<link>https://forum.nette.org/en/36840#p228598</link>
	<guid isPermaLink="false">228598@http://forum.nette.org/en</guid>
	<description>&lt;p&gt;Providers are not directly accessible, they are used within Latte extensions
&lt;a
href=&quot;https://latte.nette.org/en/custom-tags#toc-introducing-providers&quot;&gt;https://latte.nette.org/en/custom-tags#…&lt;/a&gt;&lt;/p&gt;
</description>
	<pubDate>Fri, 25 Apr 2025 10:06:52 +0200</pubDate>
</item>
<item>
	<title>Pass Variable to template (Beginners)</title>
	<dc:creator>madcoda9000</dc:creator>
	<link>https://forum.nette.org/en/36840#p228597</link>
	<guid isPermaLink="false">228597@http://forum.nette.org/en</guid>
	<description>&lt;p&gt;Hello,&lt;/p&gt;

&lt;p&gt;I&apos;m new to this and have a problem passing a global variable to all
templates.&lt;/p&gt;

&lt;p&gt;in my index.php I&apos;ve the following:&lt;/p&gt;

&lt;pre
class=&quot;language-php&quot;&gt;&lt;code&gt;/**
 * init translation util
 */
TranslationUtil::init();
$app-&amp;gt;set(&apos;trans&apos;, fn($key) =&amp;gt; TranslationUtil::t($key));

/**
 * configure latte template engine
 */
$app-&amp;gt;register(&apos;latte&apos;, LatteEngine::class, [], function (LatteEngine $latte) use ($app) {
    // translation variable intitialization
    $latte-&amp;gt;addProvider(&apos;trans&apos;, fn() =&amp;gt; $app-&amp;gt;get(&apos;trans&apos;));

    // Setze den Cache-Ordner für Latte
    $cacheDir = &apos;../cache/&apos;;

    // Prüfen, ob das Cache-Verzeichnis beschreibbar ist
    if (!is_writable($cacheDir)) {
        LogUtil::logAction(LogType::ERROR, &apos;index&apos;, &apos;latte-&amp;gt;cacheCheck&apos;, &quot;ERROR: Cache directory &apos;$cacheDir&apos; is not writable.&quot;);
        Flight::halt(500, &quot;Cache directory &apos;$cacheDir&apos; is not writable by the web server.&quot;);
    }

    $latte-&amp;gt;setTempDirectory($cacheDir);
    $latte-&amp;gt;setLoader(new \Latte\Loaders\FileLoader($app-&amp;gt;get(&apos;flight.views.path&apos;)));
});&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But when i try to use the variable in my template like this:&lt;/p&gt;

&lt;pre class=&quot;language-php&quot;&gt;&lt;code&gt;{$trans(&apos;login.username&apos;)}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I get the error, that the variable $trans is not defined.&lt;/p&gt;

&lt;p&gt;Interesting fact: when i pass the variable like this (directly to the
template), it is working.&lt;/p&gt;

&lt;pre
class=&quot;language-php&quot;&gt;&lt;code&gt;Flight::latte()-&amp;gt;render(&apos;login.latte&apos;, [
            &apos;title&apos; =&amp;gt; &apos;Login&apos;,
            &apos;sessionTimeout&apos; =&amp;gt; SessionUtil::getSessionTimeout(),
            &apos;trans&apos; =&amp;gt; Flight::get(&apos;trans&apos;),
        ]);
        return;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I hope someone can tell how i can make this variable available to aall
templates.&lt;/p&gt;

&lt;p&gt;Any hint is welcome..&lt;br&gt;
Sascha&lt;/p&gt;
</description>
	<pubDate>Fri, 25 Apr 2025 09:53:01 +0200</pubDate>
</item>
</channel>
</rss>
