View previous topic :: View next topic |
Author |
Message |
macblack77 Smarty Rookie
Joined: 29 Mar 2016 Posts: 13
|
Posted: Mon Apr 25, 2016 9:49 am Post subject: Accessing class constants |
|
|
Newb here, can someone tell me how I should be accessing class constants?
I have a notification class which can be in any of 20+ states, the current state is determined using constants (so I have 20+ constants for this).
I pass the object to Smarty using assignByRef and in the template I'm currently accessing the state using the following:
Code: | {if $notice->type==$notice::RESENDACTIVATION} |
However, I'm getting an error
Code: | Undefined variable: _tmp3 |
Which appears to be a Smarty error. I know I could pass these individually with assign, but that's a lot of code for something relatively simple. |
|
Back to top |
|
macblack77 Smarty Rookie
Joined: 29 Mar 2016 Posts: 13
|
Posted: Mon Apr 25, 2016 10:00 am Post subject: |
|
|
OK, so I found a workaround for now, but I don't understand it.
In the template I have a series of if..elseif statements. If I replace the elseif with individual if's the code works fine?? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Apr 26, 2016 1:55 am Post subject: |
|
|
To me, it sounds like you are trying to place business logic into template.
Are you absolutely sure you can't express this structure in any other way? |
|
Back to top |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
Posted: Tue Apr 26, 2016 10:20 am Post subject: |
|
|
Have you tried something like
|
|
Back to top |
|
macblack77 Smarty Rookie
Joined: 29 Mar 2016 Posts: 13
|
Posted: Wed Apr 27, 2016 10:47 am Post subject: |
|
|
AnrDaemon wrote: | To me, it sounds like you are trying to place business logic into template.
Are you absolutely sure you can't express this structure in any other way? |
Perhaps, I don't claim to be a MVC expert, but I'm using one page for simple notifications, e.g. "Your password has been changed", "Thanks your account has been activated", "Your application was successful" etc..
It would be overkill to separate these into individual templates, so instead they are all in one with the class constant used to determine which message block to display. |
|
Back to top |
|
macblack77 Smarty Rookie
Joined: 29 Mar 2016 Posts: 13
|
Posted: Wed Apr 27, 2016 11:02 am Post subject: |
|
|
elpmis wrote: | Have you tried something like
|
Thank you that worked! I don't understand it though. I thought only static members are accessed by class? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Apr 27, 2016 1:17 pm Post subject: |
|
|
Constants ARE static, per definition.
macblack77 wrote: | It would be overkill to separate these into individual templates, so instead they are all in one with the class constant used to determine which message block to display. |
Just place the selection logic back into PHP code, and use template to only SHOW the notification, not SELECT it. |
|
Back to top |
|
macblack77 Smarty Rookie
Joined: 29 Mar 2016 Posts: 13
|
Posted: Thu Apr 28, 2016 9:02 am Post subject: |
|
|
AnrDaemon wrote: | Constants ARE static, per definition.
macblack77 wrote: | It would be overkill to separate these into individual templates, so instead they are all in one with the class constant used to determine which message block to display. |
Just place the selection logic back into PHP code, and use template to only SHOW the notification, not SELECT it. |
Thanks I didn't know that..
I did start out with the selection logic in the php code, but I found it very restricting when you are trying to construct a paragraph of rich text (as in bold, italic etc), so that's why I switched to the selection logic in the template.
In my (newbie) mind, it made more sense that streams of text are presentation information and belong in the template? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Apr 28, 2016 12:41 pm Post subject: |
|
|
Construct your "rich text" in language files.
Smarty configuration mechanics are partially good as a substitution for language files. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Apr 28, 2016 10:50 pm Post subject: Re: Accessing class constants |
|
|
macblack77 wrote: | Newb here, can someone tell me how I should be accessing class constants?
I have a notification class which can be in any of 20+ states, the current state is determined using constants (so I have 20+ constants for this).
I pass the object to Smarty using assignByRef and in the template I'm currently accessing the state using the following:
Code: | {if $notice->type==$notice::RESENDACTIVATION} |
However, I'm getting an error
Code: | Undefined variable: _tmp3 |
Which appears to be a Smarty error. I know I could pass these individually with assign, but that's a lot of code for something relatively simple. |
Which Smarty version did you use? It should have worked at least with the latest release 31.29 |
|
Back to top |
|
macblack77 Smarty Rookie
Joined: 29 Mar 2016 Posts: 13
|
Posted: Fri Apr 29, 2016 11:36 am Post subject: |
|
|
I'm using v 3.1.27, I'll update when I get the chance.
If it helps, here is the markup:
Code: |
{if $notice->type==$notice::NEWLISTING}
<h3>NEW LISTING</h3>
{elseif $notice->type==$notice::REGISTRATION}
<h3>REGISTRATION</h3>
|
And here is the resulting code:
Code: |
<?php $_tmp1 = $_smarty_tpl->tpl_vars['notice']->value;
if ($_smarty_tpl->tpl_vars['notice']->value->type == $_tmp1::NEWLISTING) {?>
<h3>NEW LISTING</h3>
<?php $_tmp2 = $_smarty_tpl->tpl_vars['notice']->value;
} elseif ($_smarty_tpl->tpl_vars['notice']->value->type == $_tmp2::REGISTRATION) {?>
<h3>REGISTRATION</h3>
|
I'm very much a newb so I couldn't guess what is going wrong, to me it appears to be defining temporary variables (in this case $_tmp2) when I try to access the constants via the object. If I pull the compiled template code into PHPStorm it tells me $_tmp2 is not used and then later it tells me its not defined, this results in the template error at runtime.
If (like elpmis suggested) I access the constants via the class, then you don't see these temporary variables being created instead smarty accesses them directly with:
Code: |
$_smarty_tpl->tpl_vars['notice']->value->type == notice::REGISTRATION
|
|
|
Back to top |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
Posted: Sun May 01, 2016 10:13 pm Post subject: |
|
|
macblack77 wrote: |
If it helps, here is the markup:
Code: |
{if $notice->type==$notice::NEWLISTING}
<h3>NEW LISTING</h3>
{elseif $notice->type==$notice::REGISTRATION}
<h3>REGISTRATION</h3>
|
|
Think it would be better to move that logic to PHP and not into Smarty. In result you could assign the subtitle you needed to Smarty und use it like
Code: | <h3>{$subtitle}</h3> |
|
|
Back to top |
|
macblack77 Smarty Rookie
Joined: 29 Mar 2016 Posts: 13
|
Posted: Thu May 05, 2016 12:34 pm Post subject: |
|
|
elpmis wrote: | macblack77 wrote: |
If it helps, here is the markup:
Code: |
{if $notice->type==$notice::NEWLISTING}
<h3>NEW LISTING</h3>
{elseif $notice->type==$notice::REGISTRATION}
<h3>REGISTRATION</h3>
|
|
Think it would be better to move that logic to PHP and not into Smarty. In result you could assign the subtitle you needed to Smarty und use it like
Code: | <h3>{$subtitle}</h3> |
|
I think this is where my newbiness is showing. I've been developing my first application in PHP (in fact web development) and I've been trying to stick very much to an MVC approach.
My model contains the business rules being a series of classes and libraries. My controller deals with user interaction and process flow between the model and the view.
And my view is effectively been a series of smarty templates. If there is any presentation logic required, I've tried to achieve this using smarty.
So If I move this presentation logic (which not only selects a heading but displays one of a number of rich text paragraphs), which layer in the MVC approach does this belong to? |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu May 05, 2016 1:53 pm Post subject: |
|
|
http://lmgtfy.com/?q=why%20MVC%20is%20bad%20for%20web
"MVC" is an overloaded acronym. As a classic pattern, MVC is only applicable when you control all three instances in same code. For web it is not the case. you have at least a server and client, and you only control server.
Your actual problem is that you don't understand the distinction between presentation logic aond business logic.
Business logic answers the question "what". What happens? What needs to be rendered?
Presentation logic answers the question "how". How to render it? Where? But never - "what to render". "What to render" is answered long before the code reaches presentation layer. |
|
Back to top |
|
macblack77 Smarty Rookie
Joined: 29 Mar 2016 Posts: 13
|
Posted: Thu May 05, 2016 9:40 pm Post subject: |
|
|
AnrDaemon wrote: | http://lmgtfy.com/?q=why%20MVC%20is%20bad%20for%20web
"MVC" is an overloaded acronym. As a classic pattern, MVC is only applicable when you control all three instances in same code. For web it is not the case. you have at least a server and client, and you only control server.
Your actual problem is that you don't understand the distinction between presentation logic aond business logic.
Business logic answers the question "what". What happens? What needs to be rendered?
Presentation logic answers the question "how". How to render it? Where? But never - "what to render". "What to render" is answered long before the code reaches presentation layer. |
OK, I know what I'm doing isn't strictly MVC but I was trying to follow that pattern (albeit loosely) and I thought it was working well!
So what is considered "logic" at the presentation layer, can you give me a typical example?
If I move the above logic back to the business layer (within my series of classes) I appear to loose control over presentation (e.g. making one word in a paragraph bold or italic). The only way to do this is to include html in the business layer which seems wrong? |
|
Back to top |
|
|