开发者

Are there any techniques to separate code and markup in WordPress?

I generally work with Python to create web apps and love how I can work separately with the code and presentation开发者_开发百科 layers. I really like working with Jinja2. But, I sometimes have to work with WordPress for my clients. So, I wonder, if there are any ways to make developing for WordPress a less headache with all its header.php, index.php and footer.php as well as all other pieces that may join together well or may not. Are there any suggestions to make it more convenient?


Agreed with Denis, Wordpress simply is spaghetti, and there's nothing you can do about that. Nonetheless, if you're writing your own code for Wordpress (we make themes and plugins for our customers), there's nothing stopping you from splitting the concerns out into different files, and we found that it's much easier to maintain that way. You'll still have to coop with situations where Wordpress expects vanilla PHP files, but at least our code looks decent ;)


Sadly not. And best I'm aware, there aren't any immediate plans to adopt a more MVC-like architecture.

Some have tried to add Smarty on top and such, but doing so mostly adds bloat on top of the existing bloat. (The WP templating engine frequently consists of batches of function calls that call each other, each with their own set of plugin filters and global variables.)


Denis and Berry's answers are spot on, but it's also worth noting that there are a handful of plugins that allow for MVC design patterns within WordPress. In my completely biased opinion, the most mature is WP MVC (or see the full documentation), which uses a file structure that's very similar to those found in Rails and CakePHP.

Others MVC-based plugins include Tina MVC, Plugin Factory, and Churro.


I find putting all reusable template items as separate php, and including presentation logic as a theme function/object and business logic in a plugin in an okay way to separate concerns as such objects/functions are always available if loaded correctly. The template parts do save significant time if you use particular code all the time (like breadcrumbs for example). I know its not satisfactory, but separation of concerns is possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜