HTML & PHP in the Same Block
Permalink
I currently am in the process of migrating a site to c5. The site involves loops that create a table with data from a database.
I am wondering if it is possible to have both HTML and PHP in a block. I have looked at the few php blocks but they only allow HTML in a php echo statement.
It is pretty common practice to go out of php, type your html and then start php again. Example:
<?php PHP CODE HERE ?> HTML CODE HERE <?php MORE PHP CODE ?>
and so on.
It looks like there has been a few others in the forms trying to do the same thing with no "real" solution. The only solution is to "echo" every bit of HTML. This is a huge pain converting an existing site.
Any suggestions?
I am wondering if it is possible to have both HTML and PHP in a block. I have looked at the few php blocks but they only allow HTML in a php echo statement.
It is pretty common practice to go out of php, type your html and then start php again. Example:
<?php PHP CODE HERE ?> HTML CODE HERE <?php MORE PHP CODE ?>
and so on.
It looks like there has been a few others in the forms trying to do the same thing with no "real" solution. The only solution is to "echo" every bit of HTML. This is a huge pain converting an existing site.
Any suggestions?
![noXstyle](/files/avatars/37241.jpg)
Why would you want to do that? Wouldn't it be way easier to create a new single page to handle that? Or a custom block to show your table if you need it on various pages?
I am not sure if single pages are the best route for this site. But in general this is VERY common practice when programing php. I do not understand why it is not possible?
I completely understand why you might not want everyone who can edit the site to submit there custom php code in a block but allow Admin's to add php code in a block.
Again I am just not sure why it is not possible or why it is so hard to develop a block to do this.
I completely understand why you might not want everyone who can edit the site to submit there custom php code in a block but allow Admin's to add php code in a block.
Again I am just not sure why it is not possible or why it is so hard to develop a block to do this.
I tend to use, JordanLevs Designer Content Block to create the inputs that I want and then edit the php and html to be output in the block.
I do not ever allow someone to enter there own PHP but I edit the view so I can allow both to be displayed.
In theory you could of course adjust the view to take the text area text and output it as php. Default is to use chracter encoding on the text area values but you could remove that formating.
This package is one of the best (I believe) in the Marketplace its so versatile and makes block creation a breeze.
http://www.concrete5.org/marketplace/addons/designer-content/...
I do not ever allow someone to enter there own PHP but I edit the view so I can allow both to be displayed.
In theory you could of course adjust the view to take the text area text and output it as php. Default is to use chracter encoding on the text area values but you could remove that formating.
This package is one of the best (I believe) in the Marketplace its so versatile and makes block creation a breeze.
http://www.concrete5.org/marketplace/addons/designer-content/...
Ran across this while looking into how to use PHP inside the content block. Thanks for this. This add-on is awesome, and now essential, for me. Thanks to Jordan too for making it!
Well yeah, using inline code in .php file is a common practice whereas executing php code from database is certainly not. Creating a block with the given functionality isn't necessarily hard to do, one would just output the contents between <?php ?> tags with eval() instead. Anyhow this approach introduces unnecessary security issues to the site.
I strongly advice to take a different approach on this, e.g. the one TheRealSean just described.
I strongly advice to take a different approach on this, e.g. the one TheRealSean just described.
"Again I am just not sure why it is not possible or why it is so hard to develop a block to do this."
Ok... the quick version.
Putting PHP in one of those PHP blocks runs eval(). Since you stated that you have a fair amount of PHP experience I'm sure you realize this isn't the hottest idea. _Especially_ when interacting with the database.
Therefore when we need to have custom PHP in a custom block in Concrete5 we create a new block and/or use "Designer Content" to "scaffold" it for us as @TheRealSean suggested. Creating a new block to put some php in takes me somewhere between 2 & 5 minutes, and I'm far far from an expert php programmer.
Since Concrete5 is MVC based, you will be happy to hear that inline php is very common(to display information). What you should also know is that your database calls should be in your controller.php of your block so it can take advantage of C5's built in caching and not run every time someone views the page. Doing this can also be a bit more secure.
If you have any questions about setting up a custom block or there is something I can explain better let me know.
There is a very basic block you can use as a template here
http://www.concrete5.org/community/forums/customizing_c5/basic-bloc...
Best Regards,
Mike
Ok... the quick version.
Putting PHP in one of those PHP blocks runs eval(). Since you stated that you have a fair amount of PHP experience I'm sure you realize this isn't the hottest idea. _Especially_ when interacting with the database.
Therefore when we need to have custom PHP in a custom block in Concrete5 we create a new block and/or use "Designer Content" to "scaffold" it for us as @TheRealSean suggested. Creating a new block to put some php in takes me somewhere between 2 & 5 minutes, and I'm far far from an expert php programmer.
Since Concrete5 is MVC based, you will be happy to hear that inline php is very common(to display information). What you should also know is that your database calls should be in your controller.php of your block so it can take advantage of C5's built in caching and not run every time someone views the page. Doing this can also be a bit more secure.
If you have any questions about setting up a custom block or there is something I can explain better let me know.
There is a very basic block you can use as a template here
http://www.concrete5.org/community/forums/customizing_c5/basic-bloc...
Best Regards,
Mike