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

Allow declaring "abstract" templates

 
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 -> Feature Requests
View previous topic :: View next topic  
Author Message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Apr 06, 2015 5:34 am    Post subject: Allow declaring "abstract" templates Reply with quote

This is a wishful thinking, but here's a use case.

I have a main page template (page.tpl):
Code:
<html><head>
<title>{block 'page-title'}Silly title{/block}</title>
</head><body>{block 'page-content'}Silly content{/block}</body></html>


An intermediate template (list-index.tpl):
Code:
{extends 'index.tpl'}
{block 'page-content'}
  {block 'pager'}[1] [2] [3]{/block}
  {block 'list-content'}x{/block}
  {block 'pager'}[1] [2] [3]{/block}
{/block}


And two child templates:
content.tpl:
Code:
{extends 'index.tpl'}
{block 'page-title'}{$title|escape}{/block}
{block 'page-content'}{$content|render}{/block}

list-content.tpl:
Code:
{extends 'list-index.tpl'}
{block 'page-title'}{$title|escape}{/block}
{block 'pager'}{somepaging}{/block}
{block 'list-content'}{somecontent}{/block}


This is all fine and dandy until someone try to render index.tpl or list-index.tpl directly. Yes, amusing text that pops up subtly tells that you shouldn't be doing it, but what I'd really like to see is a SmartyCompilerException telling to never do this mistake again.

This could be implemented through additional flag "abstract" added to the block.
Blocks declared as abstract MUST have no content.
Direct attempt to render (compile) the template with abstract block declared MUST result in SmartyCompilerException.

If this functionality is implemented somehow already, can you please point me to it?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Mon Apr 06, 2015 2:53 pm    Post subject: Reply with quote

Currently there is no solution.

But I like the 'abstract' idea.

But if you have more then two inheritance levels it might still not ensure that started with the correct outer level child template. A block might be defined in the child, but overloaded in the grandchild. But it could be done with blocks only defined in the outer level. (maybe by an empty one just for this purpose)

In some future release inheritance be no longer a compile time process (for new features and more flexibility). But it could be a run time check.

I will put it on the TODO list.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Apr 06, 2015 6:16 pm    Post subject: Reply with quote

Thank you! As long as there's the functionality present in some way, I'll find a way to use it Smile
Takes a careful planning of your inheritance model, but it pay back well in the end.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Wed Apr 08, 2015 11:58 pm    Post subject: Reply with quote

That can get a bit tricky, you probably don't want to mark a block as abstract if it is already extending a block from a parent template. Maybe it would make more sense to mark templates as abstract instead of blocks.
Back to top
View user's profile Send private message Visit poster's website
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Apr 09, 2015 1:43 am    Post subject: Reply with quote

I've thought about it, but I don't see how it can be implemented, short of adding new entities, not currently present in the design.
And regarding specifically marking block abstract, that is extending another block, consider the abovementioned use case.
I may use index.tpl directly with minimal modifications, but I need to prevent intermediate list-index.tpl from being used directly, because its main purpose is to duplicate the same navigation bar at the top and bottom of the page.


Last edited by AnrDaemon on Thu Jan 11, 2018 6:31 am; edited 1 time in total
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Apr 09, 2015 2:46 am    Post subject: Reply with quote

It may be the case that the utility of an abstract template supersedes its complexity.
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: Thu Apr 09, 2015 9:34 pm    Post subject: Reply with quote

I still like the abstract block attribute to detect missing block definitions expected to be in child templates.
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 -> Feature Requests 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