Development

#4404 (Make javascript_tag() helper behave like slot() helper)

You must first sign up to be able to contribute.

Ticket #4404 (closed enhancement: fixed)

Opened 6 years ago

Last modified 6 years ago

Make javascript_tag() helper behave like slot() helper

Reported by: francois Assigned to: FabianLange
Priority: minor Milestone: 1.2.0
Component: helpers Version: 1.0.18
Keywords: Cc:
Qualification: Accepted

Description

I hate it when I have to deal with quote escaping in PHP templates. This happens a lot when I use the javascript_tag() helper, and pass my JavaScript code as a string:

<?php echo javascript_tag("
function foo()
{
  alert(\"bar\");
  return false;
}
") ?>

What would be really convenient is to adapt the javascript_tag() helper to work like the slot() helper. That way I could write:

<?php javascript_tag() ?>
function foo()
{
  alert("bar");
  return false;
}
<?php end_javascript_tag() ?>

A BC change would just have the helper act as mentioned when passed no argument. The patch is trivial. Incidentally, I would also gain JavaScript syntax highlighting in my favorite text editor.

Change History

09/18/08 12:11:30 changed by FabianLange

  • owner changed from fabien to FabianLange.
  • status changed from new to assigned.
  • qualification changed from Unreviewed to Accepted.
  • milestone set to 1.2.0.

good proposal. for 1.2 I think :-)

09/19/08 10:51:16 changed by FabianLange

here a proposal implementation

  function javascript_tag($content == null)
  {
    if ($content != null)
    {
      return content_tag('script', javascript_cdata_section($content), array('type' => 'text/javascript'));
    }
    else
    {
      ob_start();
      ob_implicit_flush(0);
    }
  }

  function end_javascript_tag()
  {
    $content = ob_get_clean();
    return content_tag('script', javascript_cdata_section($content), array('type' => 'text/javascript'));
  }

what I do not like is that there is not much error handling. Also when you mentioned that it should work like slots, were you actually requesting storing the slot contents in the request with a name, as the slot does, or just to be able to wrap as you showed?

09/19/08 10:52:32 changed by FabianLange

actually the end tag could be even nicer:

  function end_javascript_tag()
  {
    echo javascript_tag(ob_get_clean());
  }

09/19/08 11:28:34 changed by francois

Apart from the == that should be =, and the != null that should be !is_null(), I find it ok (especially with the short end tag version, which is DRYer).

What is your concern about error handling? I'm not sure I understand.

As for the slots, it's just a similar way to use the helpers. I don't see any use case justifying to store the data into slots. Or, in this case, the javascript_tag() would be called inside a slot() (!).

09/19/08 15:15:35 changed by FabianLange

dont we need a ob_end_flush() to be able to nest ob_starts?

09/29/08 11:35:04 changed by FabianLange

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [11841]) added end_javascript_tag() to JavascriptBaseHelper?.php closes #4404