Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, January 23, 2016

Is there a better way to write HTML strings in PHP?

Is there a better way to write HTML strings in PHP?


I find myself writing a lot of functions in PHP that return HTML code. They may look something like this:

function html_site_head()  {      return          "              
BARE XX DAGER IGJEN
"; }

Now I can swear I've seen better ways to write long strings in PHP. I like python's way of doing it:

return """        """  

As you don't have to escape quotation marks. An alternative is using single quotation marks, but that means no using PHP variables directly in the string. I swear I've seen something like this in PHP:

return <<<        

Or something similar. Could someone refresh my memory on this?

Thanks

Answer by Gumbo for Is there a better way to write HTML strings in PHP?


PHP knows several kinds of syntax to declare a string:

So you don?t have to use the double quotes per se.

Answer by Matteo Italia for Is there a better way to write HTML strings in PHP?


What you're looking for is heredoc syntax or nowdoc syntax.

Answer by Lee Kowalkowski for Is there a better way to write HTML strings in PHP?


Yes, it's the heredoc syntax, and I swear by it.

Answer by codaddict for Is there a better way to write HTML strings in PHP?


It is called the heredoc syntax:

return <<< END    END;  

Answer by Skilldrick for Is there a better way to write HTML strings in PHP?


As Haim said - heredocs are the way to go.

If you find yourself writing single lines, remember (or realise) that you can use single quotes for HTML attributes (don't believe anyone that tries to tell you they aren't valid).

So, e.g.:

$html = "
$contents
";

Answer by bhamby for Is there a better way to write HTML strings in PHP?


You can use a HEREDOC:

function blah() {    $bar = <<          
BARE XX DAGER IGJEN
EOS return $bar; }

?>

Answer by AmdY for Is there a better way to write HTML strings in PHP?


Better way - don't write spaghetti code, use template or

// php code  ?>    

Answer by Raftalks for Is there a better way to write HTML strings in PHP?


I like to suggest this PHP library that can handle generating just about any HTML tag with php and uses closure nesting which helps readability as well.

https://github.com/raftalks/Form

for example - a table inside a div tag

Html::make('div', function($html))  {      $html->table(function($table)      {          $table->thead(function($table)          {              $table->tr(function($tr)              {                  $tr->th('item');                  $tr->th('category');              });          });              $table->tr(function($tr)          {                 $tr->td()->ng_bind('item.name','ng-bind');              $tr->td()->ng_bind('item.category','ng-bind');                $tr->setNgRepeat('item in list','ng-repeat'); //using second parameter to force the attribute name.          });            $table->setClass('table');      });       $html->setClass('tableContainer');  });  

Answer by GoreDefex for Is there a better way to write HTML strings in PHP?


If you need a method that can be used to store HTML into a string, this is the way to do it.

I would not use any of these methods lol, I would use an Output buffer. These methods all seem mega dirty so at the risk of offending someone i'll just say the reasons I like my way is because...

When using an IDE you can still have great intelisense help with HTML and PHP separate from one another and still code out generic DOM Objects quickly.

When viewing the code it looks a lot more human readable

The HTML code can be passed in its DOM ready state rather than as a string which will eventually need to be parsed and eventually created into a DOM ready state.

When using HTML as a string method it can get really rough trying to use the various string function search and replace methods in order to change some of your code after runtime. For example looking for the tag within a massive string of other HTML elements can be daunting especially when trying to find stuff between tags...

I would use the Output Buffer Stream like so...

ob_start();  ?>        

Answer by Tarek Kalaji for Is there a better way to write HTML strings in PHP?


Sometimes for some reason is happens that PHP or Javascript or some naughty insert a lot of backslash. Ordinary function does not notice that. Therefore, it is necessary that the bit "inflate":

    RESULT: My dog don't like the postman!  

This flick has served me very well, because I had this problem before.

Source: here


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.