Welcome Guest, Not a member yet? Register   Sign In
Echoing html in php?
#1

(This post was last modified: 10-23-2015, 11:22 AM by ignitedcms.)

Hi guys,

This is one of those cases where I feel really uneasy echoing out html in PHP, and I feel kinda dirty Big Grin. I know its bad practice, because of maintainability, code syntax highlighting etc and pure ugliness, but I was just wondering if there are any cases where it is justified.

Let's give an example, I specifically writing my own html helpers, which are specific to building my management backend. I noticed the CI system files use this approach. Writing html using php, but their examples look nice mainly because their html helpers are NOT at all complicated.

Mine are, and rely on a variety of classes and ids.

So what's your opinion? Yay or Nay or is there some other way? Thanks in advance...

P.S I don't intend to use a template engine, my cms is built so users can built their own front ends using twig, my back end should remain vanilla codeigniter/PHP.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#2

(This post was last modified: 10-23-2015, 11:48 AM by InsiteFX.)

I usually create html widgets as helpers, then I just call them from the views.

This way they are all in one place for up dating etc;

./application
-- views
---- widgets

Just create a widgets view in your views folder and create your html views.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Good idea, is there any way to put them somewhere other than in the view folder, I want to keep the front end builders as separate as possible from the admin logic?
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#4

There is a good discussion here about calling views from outside the view folder. A solution further down is about outside the application folder too.

http://stackoverflow.com/questions/17358...odeigniter

Hope that is useful in some way,

Paul.
Reply
#5

(This post was last modified: 10-23-2015, 01:05 PM by ignitedcms.)

Thanks I saw that one about extending the view loader, but I'm going to plump against it anyway and create two folders in the view folder, much like insiteFX suggested.

For example I will have an Admin and custom folder, the admin widgets will be stored in the admin folder. I think this is a reasonable alternative. I hate extending the core system files mainly for migration purposes.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#6

(10-23-2015, 12:34 PM)iamthwee Wrote: For example I will have an Admin and custom folder, the admin widgets will be stored in the admin folder. I think this is a reasonable alternative. I hate extending the core system files mainly for migration purposes.

First:
Why are you against extending the core? Your reason makes no sense. You extend the core in your application directory, and not in the system directory. And even if you don't extend, migration or updating the core could still break you project.

Second:
Not extending just makes for alot more work in some cases.

Third:
Extending the loader class to use views outside the default views directory is quite easy

PHP Code:
public function custom_view_path($path$view_cascade TRUE) { 
 
   ifis_dir($path) ) {
 
       $this->_ci_view_paths = array($path => $view_cascade) + $this->_ci_view_paths;
 
   }

Reply
#7

That's a very good point Martin, you're right, however, I feel my folder structures works well at the moment.

The controller is separated into and admin and custom folder so it makes sense to mirror the same structure in the view file. But I will keep this in mind if I ever plan to push automatic updates from a website just in case.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#8

In cases where I want to generate HTML from a helper or library, I generally return the HTML, allowing the calling code to determine when it's appropriate to echo the result.

When I just want a simple partial view that I can re-use in multiple places in my application and supply with the necessary data when I load it, I just place the partial view in an appropriate directory within my views directories.

In extreme cases, I may even need a combination of the two, creating a library or helper which loads a partial view (using the optional third parameter when loading the view to return the view as a string) to format part (or all) of the HTML to be returned by the library/helper. This might be helpful if you have a theme system which allows the user (or part of the site) to load different views and/or CSS for different widgets on the site.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB