Jump to content

$page->render()


nabo
 Share

Recommended Posts

Hello

I have a problem with page render.

I need to render the same page in more language but when I call the function, the content is correctly rendered only the first time.

public function sendPost($post_id,$lang_current) {
	wire('languages')->setLanguage($lang_current);
	$post = wire('pages')->get($post_id);
	$content = $post->render();
}

If I use something like this and log the content of "en" is null... 

$this->sendPost($page->id,"default");
$this->sendPost($page->id,"en");

any idea? 😐
 

Link to comment
Share on other sites

47 minutes ago, da² said:

Hello,

Are you sure your english language name is "en"?

What is the output of this code:

foreach (wire()->languages as $lang){
    echo "$lang->name, ";
}

 

Yes, I'm sure.
I didn't post the complete example but there's a cycle that try to render page based on language sent.

If I choose any language (only one) it works, if I a choose more than one, only the first works.

Link to comment
Share on other sites

I made a quick test, and this is strange because for me the render() method always uses default language (even if language is well managed in templates on frontend).

This code works:

$languages->setLanguage('default');
echo($pages->get(6012)->title."\n");

$languages->setLanguage('english');
echo($pages->get(6012)->title."\n");

$languages->setLanguage('default');
echo($pages->get(6012)->title."\n");

But this one doesn't, it returns default language instead of english:

$languages->setLanguage('english');
echo($pages->get(6012)->render()."\n");

I'm testing by bootstrapping PW so I also tried to force output formatting and this doesn't change, but I feel this may be related with using this code from a bootstrapped code instead of a template code (because translation works in templates).

I tested and I'm right: the last code is working in a template code but not in a bootstrapped one.
So render() method looks like to have a particular behavior (bug?) with language management when bootstrapped.

Tested with PW 3.0.210.

 

EDIT:

To conclude the test, I added this code in a template, to see if multiple langage changes are working, and yes it is:

$languages->setLanguage('english');
echo($pages->get(6012)->render()."\n");

$languages->setLanguage('default');
echo($pages->get(6012)->render()."\n");

$languages->setLanguage('english');
echo($pages->get(6012)->render()."\n");

 

Link to comment
Share on other sites

8 hours ago, poljpocket said:

Two ideas, both untested:

  • Could you try resetting the language between calls? More info here (Languages::setLanguage).
  • Make sure this happens even if you supply the same language twice. This would assume there is a problem related to the render call and not the languages.

Tried both

1) yes, no change
2) as you suppose, the problem is not related to the language 😞

Link to comment
Share on other sites

7 hours ago, da² said:

I made a quick test, and this is strange because for me the render() method always uses default language (even if language is well managed in templates on frontend).

This code works:

$languages->setLanguage('default');
echo($pages->get(6012)->title."\n");

$languages->setLanguage('english');
echo($pages->get(6012)->title."\n");

$languages->setLanguage('default');
echo($pages->get(6012)->title."\n");

But this one doesn't, it returns default language instead of english:

$languages->setLanguage('english');
echo($pages->get(6012)->render()."\n");

I'm testing by bootstrapping PW so I also tried to force output formatting and this doesn't change, but I feel this may be related with using this code from a bootstrapped code instead of a template code (because translation works in templates).

I tested and I'm right: the last code is working in a template code but not in a bootstrapped one.
So render() method looks like to have a particular behavior (bug?) with language management when bootstrapped.

Tested with PW 3.0.210.

 

EDIT:

To conclude the test, I added this code in a template, to see if multiple langage changes are working, and yes it is:

$languages->setLanguage('english');
echo($pages->get(6012)->render()."\n");

$languages->setLanguage('default');
echo($pages->get(6012)->render()."\n");

$languages->setLanguage('english');
echo($pages->get(6012)->render()."\n");

 

Thanks for spending time testing this.
Unfortunately I have to use the render inside a module and it's fired after saving a page.

Don't know if this could be consider a bug @ryan?

 

 

Link to comment
Share on other sites

@da² for me, this isn't the case... For transparency, that is how I quickly put together a test file bootstrapping PW:

use ProcessWire\Languages;
use ProcessWire\Pages;

include 'index.php';

/**
 * @var Pages $pages
 * @var Languages $languages
 */

$page = $pages->get(1);
$languages->setLanguage('english');

echo $page->render();

This renders the homepage of my install perfectly using english as the language.

Maybe you post yours? Also, please attach which LanguageSupport modules you actually have installed. Is multilingual support maybe disabled on a per-template basis? What is the output strategy set for non-existent translations (in Modules > Configure > LanguageSupportPageNames)?

Link to comment
Share on other sites

I'm not using any third party module for languages.
I don't have Modules > Configure > LanguageSupportPageNames.

I only know a single way to bootstrap PW, including index.php like you do.

Getting title of the page returns it in selected language, but using render() outputs the title in default language.

For the test, be sure that your english language is not the default one, or obviously it will work.

I'm using PW 3.0.210.

 

<?php

use ProcessWire\Languages;
use ProcessWire\Pages;

/**
 * @var Pages $pages
 * @var Languages $languages
 */

include __DIR__ . "/../vendor/autoload.php";
include __DIR__ . "/../index.php";

$languages->setLanguage('english');
echo($pages->get(6012)->render('title') . "\n"); // Outputs english
echo($pages->get(6012)->render() . "\n"); // Outputs french (default language)

 

EDIT: I'm using Twig for template views.

Link to comment
Share on other sites

  • 2 weeks later...

Sorry for the late reply!

Can you post some more context about how exactly you are accessing / rendering the output with Twig?

Why am I asking this:

Your first line with render(title) does probably not use any Twig output and thus works as intended. Your second render line does use Twig! I have found, there are some problems with Twig's way of looking for methods' and properties' existence using PHP's reflection API. And this is not always very compatible with the way PW is able to "invent methods" or "add properties" using hooks. Especially stuff like Pageimages::first methods don't behave as they would in plain PHP.

In your case, there might be a problem with hooks not being executed when Twig is accessing your data. But this is just an idea.

You can try to debug this by adding something like var_dump($page->title) in your template outside of any Twig output. Then, check if the title is properly rendered in multiple languages but the Twig's content is not. If not, the problem lures somewhere else.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...