Get parameters from config.neon in template

Notice: This thread is very old.
Ja
Member | 260
+
0
-

Hi,
I have defined some parameters in config.neon, something like that:

<?php
common:
	parameters:
		contact_email: abc@abc.com
?>

how could I retrieve contact_email directly in latte template?

Thanks in advance.
Ja

greeny
Member | 405
+
+1
-

You can't do it directly.

Better create some service that will help you:

config.neon:

<script>
common:
	parameters:
		contact_email: abc@abc.com
services:
	- My\Namespace\Service( %contact_email% )
</script>

Service.php:

<?php
namespace My\Namespace;

class Service {

	protected $email;

	public function __construct($email) {
		$this->email = $email;
	}

	public function getContactEmail() {
		return $this->email;
	}
}
?>

Presenter:

<?php

...

/** @var \My\Namespace\Service @inject */
public $service;

public function renderDefault() {
	$this->template->email = $this->service->getContactEmail();
}

?>

Hope you will understand it :)

Last edited by greeny (2014-04-20 15:25)

Ja
Member | 260
+
+1
-

Yeah, I follow you, thanks :)

What if I done this with “parameters”?

<?php
services:
    - My\Namespace\Service( %parameters% )
?>

would be then accessible as an array with ‘contact_email’ ⇒ 'abc@abc.cz'?

hrach
Member | 1838
+
+2
-

Personally, I have ParametersProvider with all parameters from config, so it's easy to autowire it.

amik
Member | 118
+
0
-

Ja wrote:

Yeah, I follow you, thanks :)

What if I done this with “parameters”?

<?php
services:
    - My\Namespace\Service( %parameters% )
?>

would be then accessible as an array with ‘contact_email’ ⇒ 'abc@abc.cz'?

This is not a good idea in the manner of reusability – your service will then receive a huge array of parameters from which it needs only e-mail. See my todays earlier post which explains similar problem.