Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have to use WordPress as some meta templating system and include some JSP snipptes which will be evaluated later with Tomcat after rendering the page statically. But naturally the JSP Code could not be interpreted by the php parser. This results in plain text strings mixed with the interpreted template parts.

Is there any way to hook into the (pre) rendering of a WordPress Template?

I need way get access the rendered template so i can strip out the JSP-Tags. Maybe wrapped between some unique tags or something else. But this ist not part of this question.

Example

<?php get_header(); ?>

<render:jsp>
    <%= request.getParameter("title") %>
</render:jsp>

<?php get_footer(); ?>
share|improve this question
    
I don't quite follow your use case. If you don't want these tags in generated page source then why are you including them in template? –  Rarst Sep 30 '12 at 22:18
    
@Rarst Actually it's quite simple. I've a CMS and a Java application running on Tomcat. The CMS -WordPress- must generate static HTML with some JSP fragments which are needed by the Java application to work properly. The Client should be able to view the CMS part without being interrupted by not rendered JSP snippets. –  gearsdigital Sep 30 '12 at 22:46
1  
So you are using same output for two distinctly different purposes? Why not just generate different content for users and Java app? –  Rarst Sep 30 '12 at 23:18
    
Yes. Because this is our use case :) It's one application consisting two parts. One Part is generating jsp enhanced static content for the other one which works with the jsp. –  gearsdigital Oct 1 '12 at 7:59
add comment

1 Answer

up vote 2 down vote accepted

You can, but WP doesn't offer you the possibility to do it in a nice way, so you have to use output buffering.

Something like:

// before template is included
add_action('template_redirect', function(){

  // you can do some checks here
  // perhaps you need this only a certain page?
  if(!is_home())
    return;

  // capture output from here on...
  ob_start(function($html){

    // this function runs after the capture ends,
    // you can replace your tags here 
    $html = strtr($html, array(
      '<render:jsp>'  => '<!--',
      '</render:jsp>' => '-->',
    ));

    return $html;

  });

});

The contents of the buffer are automatically sent by PHP to the output just before the script finishes execution.

share|improve this answer
    
Thanks! I'll evaluate your suggestion :) –  gearsdigital Oct 1 '12 at 8:01
    
Works as expected. –  gearsdigital Oct 1 '12 at 9:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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