Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Template Inheritance - Variable Scope

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
M7md
Smarty n00b


Joined: 24 Mar 2015
Posts: 4

PostPosted: Tue Mar 24, 2015 12:39 pm    Post subject: Template Inheritance - Variable Scope Reply with quote

I'm not sure if this has come up before, but I search the forums and search engines with no results.

I'm creating a template for my billing software, and wanted to use Smarty 3 functionality by adding the template inheritance support.

There's 3 pages. Layout.tpl, Index.tpl, Index.php

I'm assigning a boolean in Index.php in this way:
Code:
$client = new Client();
$template->assign('isLoggedIn', $client->isLoggedIn());
$template->display("{$theme}/index.tpl");


The above code returns true/false.
Index.tpl extends Layout.tpl.

In Layout.tpl, I used:
Code:
{if $isLoggedIn}Test{/if}


And it didn't work, but if I move that code to Index.tpl, it works.

So, the "Template Inheritance" only supports local scope, and not global.

Could this be fixed?

Thanks.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Mar 24, 2015 2:26 pm    Post subject: Reply with quote

STC? I mean, simple test case? From your verbose description it isn't clear, what you are doing.
Back to top
View user's profile Send private message
M7md
Smarty n00b


Joined: 24 Mar 2015
Posts: 4

PostPosted: Tue Mar 24, 2015 5:04 pm    Post subject: Reply with quote

Example:

Layout.tpl
Code:
<!DOCTYPE html>
<html>
<head>
  <title>{$pageName}</title>
</head>
<body>
{block name=pageContent}{/block}
</body>
</html>


Index.tpl
Code:
{extends file="layout.tpl"}
{block name=pageContent}
{$pageName}
{/block}


Index.php
Code:
<?php

require_once 'libs/Smarty.class.php';

$template = new Smarty();
$template->setTemplateDir('templates/views'); // path to templates folder
$template->setCompileDir('templates/tmp'); // path the compile folder
$template->setCacheDir('templates/cache'); // path to cache folder
$template->assign('pageName', 'Hello World');
$template->display("index.tpl");


After that, if you visit index.php, you'll get the page name echoed out, but the page title would be empty.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Mar 24, 2015 9:59 pm    Post subject: Reply with quote

I can't reproduce your problem.

In your first code example you load index.tpl from a theme subfolder. {extends 'layout.tpl'} will load layout.tpl from the template root folder. So ar you sure that you load the right layout.tpl ?

Which Smarty version do you use?
Back to top
View user's profile Send private message
M7md
Smarty n00b


Joined: 24 Mar 2015
Posts: 4

PostPosted: Wed Mar 25, 2015 7:23 am    Post subject: Reply with quote

U.Tews wrote:
I can't reproduce your problem.

In your first code example you load index.tpl from a theme subfolder. {extends 'layout.tpl'} will load layout.tpl from the template root folder. So ar you sure that you load the right layout.tpl ?

Which Smarty version do you use?


I'm using the latest version of Smarty.
The path to the layout file is correct, since all the HTML source shows up on View Source on the browser.

The problem here is that if you assign a value to a variable, and then display the index.tpl, that variable won't get assigned to the "extended file = layout.tpl".

So the variable is local to only the displayed file, but the extended file doesn't receive the variable value.
Back to top
View user's profile Send private message
M7md
Smarty n00b


Joined: 24 Mar 2015
Posts: 4

PostPosted: Wed Mar 25, 2015 7:30 am    Post subject: Reply with quote

U.Tews wrote:
I can't reproduce your problem.

In your first code example you load index.tpl from a theme subfolder. {extends 'layout.tpl'} will load layout.tpl from the template root folder. So ar you sure that you load the right layout.tpl ?

Which Smarty version do you use?


I also fixed the problem, but it's not that clean.

The solution is adding the line below after {extends}:
{assign var="isLoggedIn" value="$isLoggedIn"}
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Mar 26, 2015 12:20 am    Post subject: Reply with quote

All what you said does not make any sense. In Smarty 3.1 inheritance is a compile time process where the {block}{/block} sections are merged together that the output of the compiler is one template which has just one context.

Can you please provide the content of the compiled template file xxxx.file.index.tpl.php from the template_c folder.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP