Adam
Adam

Reputation: 518

Using PHP in the midst of markup

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

Answers (3)

AbsoluteƵER&#216;
AbsoluteƵER&#216;

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.

Heredoc Example:

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:

PHP wrapper for HTML:

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;

HTML with PHP spread throughout.

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

cHao
cHao

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:

  • Have the function take those variables -- at which point you can return actual HTML rather than a bunch of pieces that the caller then has to assemble.
  • Make the widget's HTML a file, and include it. The HTML can then use PHP snippets to echo any variables that are defined at the point where you include it.
  • Use a template system (or even something like 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

rady
rady

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

Related Questions