0

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(); ?>
4
  • 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, 2012 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. Sep 30, 2012 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, 2012 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. Oct 1, 2012 at 7:59

1 Answer 1

2

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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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