Hi!
I'm currently porting Wordpress plug-in to Movable Type 4 and I need
a way to inject links to JavaScript/CSS files into published HTML
<head> from plug-in without user's assistance. Within Wordpress this
is done with call to 'wp_enqueue_script()' and 'wp_register_style()'
functions.
I've tried two approaches to this problem and both from within
'build_page' callback. First method is simple regex substitution:
${$args{'Content'}} = s/(.*)<\/head>(.*)/$1$javascript<\/head>$2/i;
This works but this methods seems crude and unreliable for me - we
can't be sure that matching </head> tag is real one (it can be
within comments) or if $args{'Content'} is HTML at all.
Second approach is modifying $args{'Template'} by adding necessary
JavaScript/CSS into <mt:SetVarBlock name="html_head" append="1">
through DOM modification and rebuilding HTML from template
afterwards.
${$args{'Content'}} = $args{'Template'}->build( $args{'Context'} );
While this method seems working too, but I'm getting other headers
duplicated - looks like appending "html_head" blocks from other templates
getting executed twice. I have tried dropping args{'Context'} from
above call but this gets me entirely empty Entry page.
So I have following questions:
- First with regards second approach - Is there way to rebuild page
inside 'build_page' callback properly without side-effects from
previous build? Is this approach is worth pursuing at all?
- Is there any better approach to this task? I've tried to find
similar examples in other plug-ins but almost without luck. Some
plug-ins use similar method for injecting JavaScript into
"html_head" but only from template_param hooks which is
appropriate (if I'm not mistaken) only for dynamic pages like
mt-comment.cgi or CMS backend and for published pages they just
ask user to insert necessary JavaScript/CSS links into Header by
hand - for example, see READMEs from plug-ins by Byrne Reese [1]
and Beau Smith [2]. Is asking user to manipulate complex templates
really most reliable and convenient way to inject necessary links
into generated HTML?
[1] - http://github.com/byrnereese/mt-plugin-assetgallery
[2] - http://github.com/sixapart/mt-plugin-facebook-connect-commenters
Reported on Movable Type 4.3
1 Reply
| Add a ReplyAdd a Reply