Re: Re: Preferred Syntax

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 16 Dec 2011 23:53:46 -0500, Eric Butera wrote:

>To all the people who responded to this thread:
>It is 2011 - please stop writing code like this.
>
>To the OP:
>I'm glad you're asking questions and realizing you're not happy with
>your current abilities and suspect there's a better way.  I've read
>the replies in this thread and feel a bit let down.  Use a templating
>language - yes, I understand that is what php is for - but I won't go
>into it.  You should not be echoing, printing, or any other method of
>concatenating html dealing with escaping quotes inside your php logic
>code.  Please separate your concerns.

By and large, I agree with you, and certainly it is a good idea to raise
that with the OP since they were asking about recommended practices.
However, "should not" is not the same as "don't" and there are still
places where this is appropriate. 

Here's a chunk of real code from a WordPress plugin, taken from the
class that extends the Page admin form to provide a button for selecting
a custom associated image:

echo <<<HTML
<input type="button" class="button mmm-admin-image-button"
data-fieldname="$fieldName" value="Select"
id="mmm-admin-image-button-$fieldName" />
<input type="hidden" name="$fieldName" value="$fieldValue" />
<span id="mmm-admin-image-caption-$fieldName"
>{$this->getAttachmentCaption($fieldValue)}</span>
<input type="button" class="button mmm-admin-clear-button"
data-fieldname="$fieldName" value="Clear" />

HTML;

HEREDOC provides me a way to do templating in a simple way within a
WordPress hook function. The purpose of the hook is to provide a way to
augment WordPress Page editing by adding some HTML; sending a small
fragment of HTML to the browser is its function. I gain nothing by
spinning that fragment out to a template file somewhere.

For other hook functions (e.g. representing shortcodes), it is
beneficial to use templating, and I do so -- but horses for courses.

>Not sure what that means?  That's OK!  If you want to move forward,
>look up how modern frameworks deal with this issue using their views
>or template views.  You don't have to use a framework if you do not
>want to, that's perfectly fine.  If it works, it works.  But in the
>end, it the separation of logic and html is essential to code
>maintenance.

Applause! :)
-- 
Ross McKay, Toronto, NSW Australia
"The chief cause of problems is solutions" -Eric Sevareid

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux