I'm writing a shortcode for a child-theme.

I want to load a php template file to generate my HTML and then output this HTML in a string to return this string. But I don't know the best way to do this. The Codex mentions the ob_start function but it seems to me a bit dirty.

Are there any template functions (like those in PHP frameworks when you load a view) to do this?


WordPress doesn't bundle any "templating engine" libraries like Mustache or Smarty, as far as I am aware, and frankly I have not been sold on the need for them. (Yes, I know its all the rage.)

Concatenate your own strings, use PHP native functions like ob_start or sprintf, or load your own templating library. I have seen plugins do that. I just recently saw one that loaded Mustache, and there is a WordPress plugin purporting to make Smarty available.

| improve this answer | |
  • Thank you, it's a complete answer ! I'll use ob_start, I don't want to concatenate more than 50 HTML lines. Hopefully one day WP will publish this feature. – RChanaud Aug 5 '13 at 13:55
  • I hope WordPress doesn't incorporate this additional layer of code that has to be read, parsed, and executed. – s_ha_dum Aug 5 '13 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.