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

cache_id=null for subtemplates.

 
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 -> General
View previous topic :: View next topic  
Author Message
moroz1999
Smarty Rookie


Joined: 02 Mar 2011
Posts: 5

PostPosted: Wed Sep 26, 2018 8:51 pm    Post subject: cache_id=null for subtemplates. Reply with quote

I have a generic structure in template:
{include file=$module->getTemplate() cache_id=$module->getCacheId() cache_lifetime=$module->getCacheLifetime()}

$module can be an instance of many different CMS classes, each one having it's own implementation of getCacheId and getCacheLifetime methods.
Some of module should never be cached. Is it possible for getCacheId() to return some value which won't enable the caching for this single include?
For example, if module returned false or null, then caching would be ignored exactly as if there was totally no cache_id and cache_lifetime provided.

I know that one of the possible ways would be using such structure:
{if $module->getCacheId()}
{include file=$module->getTemplate() cache_id=$module->getCacheId() cache_lifetime=$module->getCacheLifetime()}
{else}
{include file=$module->getTemplate()}
{/if}

But this makes code unneededly longer.

As far as I can see in smarty/smarty/libs/sysplugins/smarty_internal_compile_include.php:168, Smarty doesn't control the value of attribute, it only controls if it's isset.
if (isset($_attr[ 'cache_id' ])) {
$_cache_id = $_attr[ 'cache_id' ];
$call_nocache = true;
$_caching = $_new_caching;
} else {
$_cache_id = '$_smarty_tpl->cache_id';
}

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


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Sep 27, 2018 5:06 pm    Post subject: Reply with quote

Make your modules implement some "render" method instead, which includes and sets the template.
Then
Code:
$smarty->registerPlugin('function', 'someName', [$module, "render"], $module->isCacheable(), $module->getCachedAttributes());

And just use {someName} in templates.
Back to top
View user's profile Send private message
moroz1999
Smarty Rookie


Joined: 02 Mar 2011
Posts: 5

PostPosted: Thu Sep 27, 2018 5:34 pm    Post subject: Reply with quote

Thanks for suggestion and a quick reply.
I'm afraid that I have too many different modules in system which can be combined together in the same template. Since it's a CMS, it can display in same answer different modules:
login form
basket
product
category
some modules from column
subscription form in footer
and many-many others.
Also, the same module can be used as different instances (for example, products in catalogue, where each product block represents the separated instance of "product" module, each one having it's properties like ID, price, title, modification date).

This is why I won't be able to use a custom function for this purpose, since I would have to register separated functions for each module's instance and somehow predict their usage in templates.

I've made a small proposition to illustrate what I mean:
https://github.com/smarty-php/smarty/pull/492

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


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Sep 27, 2018 6:16 pm    Post subject: Reply with quote

moroz1999 wrote:
Since it's a CMS, it can display in same answer different modules:

Yes, it can display different modules. So, what?

Quote:
This is why I won't be able to use a custom function for this purpose, since I would have to register separated functions for each module's instance and somehow predict their usage in templates.

Yes. Just register them all. I don't see a problem.

moroz1999 wrote:
I've made a small proposition to illustrate what I mean:
https://github.com/smarty-php/smarty/pull/492

I don't quite get the meaning of your PR, not to mention it's principally broken.
Why should template engine adapt to your messy code?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Sep 27, 2018 6:56 pm    Post subject: Reply with quote

Though, you can create a single modifier, which would render any $module you like up to your expectations.
Back to top
View user's profile Send private message
moroz1999
Smarty Rookie


Joined: 02 Mar 2011
Posts: 5

PostPosted: Thu Sep 27, 2018 7:12 pm    Post subject: Reply with quote

I'm not surprised there is not much activity here with such an attitude. Why acting so aggressively and arrogantly? That's a support forum, I'm not forcing anybody to do anything, I've came here for a goddamn help, not to teach anyone how to code.
I'm a smarty user for about 12 years already, why can't you just be polite and act professionally?

I'm not convincing anybody my PR was perfect. That's a request to illustrate the idea, not a demand. That's the last thing I was waiting to meet here, really, what a disappointment.

According to documentation, cache_id has to be string/integer. Can it be null? No idea.
https://www.smarty.net/docs/en/language.function.include.tpl
I understand that was wrong from my side to offer empty() because it skips the zero integer, but please update your docs to include null as well if you want this to be understood by the developers. You can just add a simple mentioning of "Null shall cache subtemplate with no cache_id", that would be enough.

Thanks to comments on PR I understand now the hidden ideology behind these attributes and I will find a way to get used to it.
I'm not going to convince you about "different modules" conception, since you don't really seem to be willing to help after all, do you? So why bothering spend our both's time for such a "support"?

Have a nice day!
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Sep 30, 2018 12:02 pm    Post subject: Reply with quote

Arrogance? Aggression? Where? Are you placing your own attitude into my words? Then blame yourself for being aggressive and arrogant.

I suggested a straightforward solution, two even. The last one explicitly solves your problem with least fuss.

If you don't want to learn, then the only explanation for your appearance is that you were trying to make somebody do your work for you. This is not what this forum is about. See the "Help wanted (commercial)" section for that kind of "help".
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 -> General 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