Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access renderBlock method? #35

Closed
goosechaser opened this issue May 18, 2013 · 4 comments
Closed

How to access renderBlock method? #35

goosechaser opened this issue May 18, 2013 · 4 comments

Comments

@goosechaser
Copy link

Hi, I'd like to use the following method from Twig's \vendor\twig\twig\lib\Twig\Template.php;

public function renderBlock($name, array $context, array $blocks = array())

The following doesn't work:

$view =  View::make('twigtemplate', $data);
return $view->renderBlock("block");

Thanks.

@rcrowe
Copy link
Owner

rcrowe commented May 20, 2013

The Twig Template class Twig_Template is the class that the generated php files based on your templates extend, see TwigBridge\Twig\Template

It's therefore not an instance you will have access to through the normal View facade.

Can I ask what your trying todo?

@goosechaser
Copy link
Author

Thanks. I have bunch of blocks in my twig templates. For example:

{% block ajaxrequest %}
code...
{% endblock %}

If I detect an AJAX request, I'd like to render only that particular block and return to client. Also, there are useful methods like hasBlock, renderParentBlock, etc. that I'd like to access for other purposes and I'm not sure how.

@rcrowe
Copy link
Owner

rcrowe commented May 20, 2013

Couple of ways you can do the ajax stuff:

{% if request_ajax() %}
    ...
{% endif %}

Maybe even checkout Turbo :p

Access to the generated class isn't something that TwigBridge is likely to provide for you. More than welcome to provide a pull request if you manage to do so.

@rcrowe rcrowe closed this as completed May 20, 2013
@goosechaser
Copy link
Author

I do it like this now:

$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader);
$template = $twig->loadTemplate(View::make('template')->with("data", $data)) ;
return $template->renderBlock("block", array());

It works but render time increases from 15ms to 83ms when I run a test. displayBlock() shaves a couple of ms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants