Having <?php tags inside a js .html() method?
If we have:
html('<a href="blabla.org">go here</a>')
开发者_C百科Can we have <?php
tags inside inside href on this case, or it's a bad practice?
Thanks. MEM
You can have those, sure. Just know that the code inside it will be executed on the server, before the JS is executed. So if you have:
html('<a href="blabla.org"><?php echo 'go here'; ?></a>')
Your Javascript will become:
html('<a href="blabla.org">go here</a>')
You can't influence the PHP code at the time the JS is executed.
theres nothing wrong with it but we do like to have some standards on writing php within view logic.
Firstly you should familiarize your self while
/ for
/ if
/ else
/ foreach
loops within the alternative syntax.
This gives a more clearer prospective on html / php combined code., Example Below
<?php if ($a == 5): ?>
<b>HTML</b> Here <br />
<?php endif; ?>
Theres also another trick that helps you keep your code clear and that's short tags but its not switched on in most conmen php server configurations so you have to make sure
html('<a href="blabla.org"><?=$blabla?></a>')
Have a look for yourself:
http://php.net/manual/en/control-structures.alternative-syntax.php
精彩评论