Codeigniter template libraries, with partial views - which one?

风格不统一 提交于 2019-12-10 19:09:29

问题


I started researching this topic for a couple of reasons...

1) To reduce repetitive code in my controllers 2) To configure 3 main layouts used in my website - 1 column, 2 column and 3 column pages 3) To be able to have separate partial view files, and load them accordingly according to the template (main-content, header, footer and sidebar)

I have googled this topic and come up with very few examples with user-friendly documentation, and I was hoping for some guidance.

http://net.tutsplus.com/tutorials/php/an-introduction-to-views-templating-in-codeigniter/ by Sam Stevens is great... but lacks the dynamicism needed to load individual view files for each template.

https://github.com/jenssegers/CodeIgniter-Template-Library by Jens Segers just confuses the life out of me with this unnecessary widget malarkey.

http://philsturgeon.co.uk/blog/2009/12/CodeIgniter-Template-library by Phil Sturgeon looks good... but lack of docs and bugs make it very difficult for a newbie like me to get up and running.

http://williamsconcepts.com/ci/codeigniter/libraries/template/index.html by William Concepts seems to be the best from what I can gauge from other users - but it's not out-of-the-box ready, and I am getting errors about trying to define the likes of header twice! It's just not working for me.

If someone can advise me which library (or other one) would best fulfill what I'm trying to achieve I could then put all my attention into implementing and understanding it, instead of jumping between examples...

Also, all of these examples seem to be (originally) YEARS old (bar Sam Steven's) which makes me wonder, is there not newer or more recent examples available?


回答1:


A very similar question has been asked regarding which templating library is best. I would even consider flagging this as a duplicate question, but since you asked "is there not newer or more recent examples available?" -- I'll bite.

You should try and use Stencil. It was released this year, is very well documented, and I use it on all my CodeIgniter projects (full disclosure: I am the creator).

Some of it's features are:

  1. Easily set titles
  2. Simple Layouts
  3. Page-specific assets (js and css, perfect for jQuery plugins)
  4. Page-specific meta data
  5. Tons of HTML5 helpers
  6. Flexible ways of binding data to views
  7. Slices (also called partials, includes, nested views, elements, etc)
  8. Slice Callbacks (also called view models where you can execute a block of code every-time a Slice is used)

Again, this is something I built though. It's all about finding the right tools for the job, and you should research what works best for your project.

Links

GitHub | Docs | Demo




回答2:


This is more of an opinion based question rather than one someone can factually answer. There is not the best but just different tools for the job. First I will say that the reason why you may only be seeing years old libraries for CI is because many have jumped shipped to newer frameworks that use namespacing and newer php 5.3 features. This is not saying CI is bad, I still use it for a few of my projects.

Phil Sturgeon used to be a core contributor to CI but has now moved on to Laravel if I remember correctly. (Laravel is also what I have been migrating to as well)

Tutsplus is a great site filled with awesome information. In fact just recently they have a new premium course for CI: https://tutsplus.com/course/codeigniter-best-practices/

Beyond those I do not know of the other two and cannot give you any information. Me, I do not use any template libraries and just give each partial view a name/index in an object and if the name exists then I call the partial view and load it with the properties in the object with the same name (header, css, script, content, footer, etc). Then you just have a template view that acts as a .net master page and you just inject your partial views into it. You can also do this by creating a hook. Perhaps a google search for CI template hooks as well.




回答3:


All the examples of this page are not maintained. For templating in CodeIgniter, you can use the CI Template Parser Class, but is very lean.

Otherwise, you can use Smarty or Twig, they have a very powerful feature for partial views: template inheritance.

Template inheritance is an approach to managing templates that resembles object-oriented programming techniques. Instead of the traditional use of {include ...} tags to manage parts of templates, you can inherit the contents of one template to another (like extending a class) and change blocks of content therein (like overriding methods of a class.) This keeps template management minimal and efficient, since each template only contains the differences from the template it extends. Template inheritance allows you to build a base "skeleton" template that contains all the common elements of your site and defines blocks that child templates can override.



来源:https://stackoverflow.com/questions/17571512/codeigniter-template-libraries-with-partial-views-which-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!