开发者

is there a way to grab variable in body tag and use it in head tags

I am trying to supply the title tag for facebook open graph with custom content for the s开发者_运维技巧end button. As you know the meta for the open graph is in the head tags and the information is in a variable in the body, therefore I need to use the variable before it's declared. Hints the problem.

I have searched around and tried a few of my own ideas and had no luck. Is there a way either with php, jquery or ajax to grab the variable and use it in the head tags?

Thanks guys.


Here is how I would do it..

I would write a controller file and put the head content and body content into arrays, then I would search for naming conventions or functions in the body.. then add the function or variable to the head array.

Finally, build the template. arrays:

$head = array();
$body = array();

$head[] = '<title>My Title</title>';
$head[] = '<script>some script here</script>';
$body[] = '<h3>Hello World</h3>';
$body[] = myfunction();

Now you can search the $body array with a pregmatch looking for myfunction or if you prefix the variables with $head_myvariable you can search for head with pregmatch.. if it exists.. Simply add the variable to your head array:

$head[] = $mybodyvariable;

Now you can publish everything:

foreach($head as $h) {
     echo $h;
}
foreach($body as $b) {
     echo $b;
}

You will need to do a little research on preg_match as the regex is different for everytype of search.. so google this php: preg_match

Good luck, hope that helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜