Reputation: 518
I am currently creating a social buttons function that I will be calling elsewhere. However, my issue is mixing php with markup.
function _testsite_social_buttonz() {
return array(
'#prefix' => '<div class="toolbar-social">',
'#suffix' => '</div>',
'social_button_facebook' => array(
'#type' => 'markup',
'#markup' => '<div id="facebook" data-url="http://testsite.com<?php print $node_url; ?>" data-text="<?php print $title; ?>"></div>',
),
'social_button_twitter' => array(
'#type' => 'markup',
'#markup' => '<div id="twitter" data-url="http://testsite.com<?php print $node_url; ?>" data-text="<?php print $title; ?>"></div>',
),
);
}
Is the code I currently have. I have attempted this as well with no luck
function _testsite_social_buttonz() {
return array(
'#prefix' => '<div class="toolbar-social">',
'#suffix' => '</div>',
'social_button_facebook' => array(
'#type' => 'markup',
'#markup' => '<div id="facebook" data-url="http://testsite.com' .<?php print $node_url; ?> . " 'data-text=" .<?php print $title; ?> ."></div>',
),
'social_button_twitter' => array(
'#type' => 'markup',
'#markup' => '<div id="twitter" data-url="http://testsite.com' . <?php print $node_url; ?> . " 'data-text=" . <?php print $title; ?> . "></div>',
),
);
}
Is there a way to incorporate both? Or will i have to switch to something like #value and get a workaround that way instead?
Thank you
EDIT: So I have gotten the mix of html and php yet I can't get these values to either show or fill with the right information. I currently now have
function _test_social_buttonz() {
$node = node_load($nid);
$url = "$node_url";
$title = $nid->title;
return array(
'#prefix' => '<div class="toolbar-social">',
'#suffix' => '</div>',
'social_button_facebook' => array(
'#type' => 'markup',
'#markup' => '<div id="facebook" data-url="http://test.com' . $node_url . '" data-text="'. $title . '"></div>',
),
'social_button_twitter' => array(
'#type' => 'markup',
'#markup' => '<div id="twitter" data-url="http://test.com' . $node_url . '" data-text="'. $title . '"></div>',
),
); // End return statement
} // End _test_social_buttons() function
SOLVED : Here's how I ended up doing it.
function _testsite_social_buttonz() {
if ((arg(0) == 'node') && is_numeric(arg(1))) {
$node = node_load(arg(1));
}
$url = $_SERVER['REQUEST_URI'];
$title = $node->title;
return array(
'#prefix' => '<div class="toolbar-social">',
'#suffix' => '</div>',
'social_button_facebook' => array(
'#type' => 'markup',
'#markup' => '<div id="facebook" data-url="http://testsite.com' . $url . '" data-text="'.$title .'"></div>',
),
'social_button_twitter' => array(
'#type' => 'markup',
'#markup' => '<div id="twitter" data-url="http://testsite.com' . $url . '" data-text="'.$title .'"></div>',
),
); // End return statement
}
Upvotes: 1
Views: 1185
Reputation: 7870
There are multiple ways to do this and it comes down to user preference. There have been several "studies" and there are no speed or processing benefits from one to the other. Biggest factors are going to be user preference, skill, and familiarity. If you're working with a team make sure you're all on the same page.
In the PHP manual checkout Heredoc. In my experience I've found it's one of the best ways of switching between multiple languages all inline.
This one is cool because you can print it or store it in a var for later use, or write it to a file.
<?php
$code = <<<EOD
<div>$someText</div>
EOD;
echo $code;
?>
Other ways to pull it off:
Like Heredoc you can print, store in a var for later use, or write it to a file:
$code = "<div>\n";
$code .= "$someText";
$code .= "</div>\n";
echo $code;
This one is more complex if you're using PHP, XML, HTML, Javascript, Jquery, and who knows what else all intermixed. To work this way you would need an excellent editor that does tagging across multiple languages.
<div>
<?php echo $someText; ?>
</div>
Upvotes: 3
Reputation: 86506
You can't echo out PHP snippets and have them parsed by PHP. (Not without doing something shady, anyway.) By the time the code runs, the parsing step is already done -- and any PHP you print out after that will just be sent to the browser as is.
Instead, you could do any of these:
include
it. The HTML can then use PHP snippets to echo any variables that are defined at the point where you include it.preg_replace_callback
) that can replace variable markers in your text, and add those markers. The caller can then set those variables before rendering the template.or a number of others.
Upvotes: 1
Reputation: 428
you don't need to write <?php . . .?>
you just need to call the variable. so instead of writing "mystring".<?php print $myvar;?>;
you just need "mySting".$myvar;
Upvotes: 3