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
Goto page Previous  1, 2
 
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 -> Smarty 3
View previous topic :: View next topic  
Author Message
U.Tews
Administrator


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

PostPosted: Fri Mar 27, 2009 6:54 pm    Post subject: Reply with quote

Okay no big deal to change it. will do it tomorrow.
Back to top
View user's profile Send private message
yankee
Smarty Rookie


Joined: 02 Mar 2009
Posts: 31

PostPosted: Fri Mar 27, 2009 7:08 pm    Post subject: Reply with quote

Reading this text I thought of a new type of {assign}-Statement like this:
{assign var='foo'}bar{/assign}

Note that the value of $foo does not come from a value attribute, but from code within the function. Internally this could be done by using php's output buffering functions.
This architecture allows something like this:

Code:
{assign var='head'}<!-- put css /javascript stuff here -->{/assign}
{assign var='body'}<!-- put content here -->{/assign}

<html>
<head>
{$head}
</head>
<body>
{$body}
</body>
</html>

Of course the obove assign code can come from an included template. And it could be made possible to write down the inclusion chain in the controller:
Code:

$head =$smarty->createTemplate('head.tpl');
$body =$smarty->createTemplate('body.tpl');
$out =$smarty->createTemplate('master.tpl');
$out->addPreRun($head);
$out->addPreRun($body);
$out->display();


This should give more possibilities to the user while decreasing the complexity of smarty's language...

And one more thing:
Code:
$smarty->display('extend:base.tpl|foo.tpl');

This is a language within a language. The problems that come with it are already known to us by other languages that work like that (for example SQL).
I'd prefer to write such things in php.. Either like this:
Code:
$smarty->extend('array('foo.tpl', 'bar.tpl', 'foobar.tpl'));

Or by something that looks similar to the "addPreRun"-Code above.
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Fri Mar 27, 2009 7:29 pm    Post subject: Reply with quote

yankee,

capture seems to do the same thing as the assign you mentioned

http://www.smarty.net/manual/en/language.builtin.functions.php#language.function.capture

every one,

I was wondering if {block}s can be nested in the superclass. I realize this makes things more complicated to implement, but at the same time, it could allow the user to change the entire page, or change things on a smaller granularity.

Problem is, I don't think nesting would make sense in a subclass, I don't know.

perhaps there could be two functions, block (for super-templates) and block_replace (for sub-templates)

Question Question Question
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Mar 27, 2009 8:06 pm    Post subject: Reply with quote

Quote:
I was wondering if {block}s can be nested in the superclass. I realize this makes things more complicated to implement, but at the same time, it could allow the user to change the entire page, or change things on a smaller granularity.


I don't think that will happen. You are correct, it gets too complex. I think you can build your templates with building blocks that extend each other to get the combination you want.

A lot of the feature inspiration came from django and there is probably a reason why they don't nest them either.
Back to top
View user's profile Send private message Visit poster's website
yankee
Smarty Rookie


Joined: 02 Mar 2009
Posts: 31

PostPosted: Fri Mar 27, 2009 8:21 pm    Post subject: Reply with quote

douglassdavis wrote:
yankee,

capture seems to do the same thing as the assign you mentioned

Yes, you are right... At least it's nearly the same.

douglassdavis wrote:
I was wondering if {block}s can be nested in the superclass. I realize this makes things more complicated to implement, but at the same time, it could allow the user to change the entire page, or change things on a smaller granularity.

Well... You could do this using variables Wink.
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Fri Mar 27, 2009 9:17 pm    Post subject: Reply with quote

yankee wrote:
douglassdavis wrote:

douglassdavis wrote:
I was wondering if {block}s can be nested in the superclass. I realize this makes things more complicated to implement, but at the same time, it could allow the user to change the entire page, or change things on a smaller granularity.

Well... You could do this using variables Wink.


You can do many things with variables... Smile But, explain how please.


From what I'm thinking, some one could do something like:

base.tpl
Code:

{block name='sentence'}The {block name='color'}brown{/block} fox jumped over the dog.{/block}


my.tpl
Code:

{block name='color'}red{/block}


Produces:
Code:

The red fox jumped over the dog.


So, from that the user can choose to change the entire sentence OR the color of the fox when the template is inherited. I can think of a few things I've done where this would be useful.

The problem is if some one did not nest the same in a sub-template, I'm not sure what the meaning would be:

Code:

{block name='color'}Some text{block name='sentence'}more stuff{/block} the last bit{/block}


My interpretation would be similar to in PHP when A() calls B(), and you subclass it, override A() and B() and make B() call A(). but, i could see how it could be trouble.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Fri Mar 27, 2009 11:23 pm    Post subject: Reply with quote

Nesting of blocks is not allowed for good reasions....
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sat Mar 28, 2009 8:53 am    Post subject: Reply with quote

The file order has been changed:
Code:
Smarty->display('extend:base.tpl|section.tpl|my.tpl');
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Jun 09, 2009 8:43 pm    Post subject: Reply with quote

so..

can a super template include other templates?

if so, presumably included files would be in the same dir as the super-template? how would that happen, since the template dir is that of the sub-template?

would a new template dir be established within the super-template as soon as you do an {extend}? That is, if I make a super template, can i count on the template dir to be the same as the super-template's dir?

Otherwise, how do includes in the super-template work?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sat Jun 13, 2009 9:25 pm    Post subject: Reply with quote

The super template (base.tpl) can include other templates. The template_dir is use same way as on any other template.
Back to top
View user's profile Send private message
v2k
Smarty n00b


Joined: 13 Oct 2009
Posts: 1

PostPosted: Tue Oct 13, 2009 2:11 am    Post subject: Reply with quote

Is there any documentation or examples for using inheritance in smarty 3? Beyond this thread, I can't seem to find anything.
Back to top
View user's profile Send private message
onamali
Smarty n00b


Joined: 29 Nov 2009
Posts: 2
Location: Malta, EU

PostPosted: Sun Nov 29, 2009 11:35 am    Post subject: Reply with quote

As of Smarty 3 Beta 5 the correct code for using extends programatically is

Smarty->display('extends:base.tpl|section.tpl|my.tpl');

Please note the resource name is EXTENDS not EXTEND as listed in the previous examples.

Example

index.php
Code:
$page='home'
$smarty=new Smarty();
// Other smarty setup code here
$smarty->display('extends:base.tpl|'.$page.'.tpl');

base.tpl
Code:
<html>
<head>
<title>{block name="title"}{/block}</title>
</head>
<body>
{block name="content"}{/block}
</body>
</html>

home.tpl
Code:
{block name="title"}Home Page{/block}
{block name="content"}This is the content of the home page{/block}
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


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

PostPosted: Sun Nov 29, 2009 6:47 pm    Post subject: Reply with quote

See also a brief description in the README file.
Back to top
View user's profile Send private message
onamali
Smarty n00b


Joined: 29 Nov 2009
Posts: 2
Location: Malta, EU

PostPosted: Sun Nov 29, 2009 8:31 pm    Post subject: Reply with quote

U.Tews wrote:
See also a brief description in the README file.

Thanks, I forgot about it being there!

Link to README
http://smarty-php.googlecode.com/svn/branches/Smarty3Dev/distribution/README
Back to top
View user's profile Send private message Visit poster's website
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 -> Smarty 3 All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
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