Using a session variable in a latte file

Notice: This thread is very old.
kc2scy
Member | 22
+
0
-

Hello,
I need to capture the user who log in email address and I like to display on my home page.
so when the user log in I wrote it to a session variable like below:

$section = $container->getService('session')->getSection('email');
$section->email = $values->email;

if I did this correctly I thought I could access it this way:

{var $section = $container->getService('session')->getSection('email')}

   <p id="_email " class="_email"> {$section->email} </p>

however I get a error :

Undefined variable: container

I thought that the $container was singleton so I didn't have to declare it?
any help would be great.

Thanks
Mike

Last edited by kc2scy (2014-02-02 01:54)

Patrik Votoček
Member | 2221
+
0
-

Just pass it into template from your presenter

class HomepagePresenter extends BasePresenter
{
	/** @var \Nette\Http\Session */
	private $session;

	public function __construct(\Nette\Http\Session $session)
	{
		parent::__construct();

		$this->session = $session;
	}

	public function renderDefault()
	{
		$this->template->userEmail = $this->session->getSection('email')->email;
	}
}
<p id="_email " class="_email"> {$userEmail} </p>
kc2scy
Member | 22
+
0
-

Thank you, I just leaning the framework.

kc2scy
Member | 22
+
0
-

Hello Patrik,

Thanks it worked with a simple presenter but when I added it to a presenter with a form I got stuck.
What I have is a name of a image in my database and I'm trying to pass back the path to where the image is. I wanted it to be display on the same template as the form.

What I have is this:

Template:

{block content}


<div id="signin" class="signin">
{control profileForm}
</div>

<div>
 <img src="{$profilePic}" alt="No Image" height="200" width="300">
</div>

**
Presenter:**

public function renderDefault()
    {
       $this->template->profilePic = './images/'. $this->id. '/'.$this->client->image;

    }

protected function createComponentProfileForm()
	{

        if (!$this->user->isLoggedIn()) {
            $this->redirect('Sign:in');
        }


        $id = $this->getUser()->getIdentity()->getId();

        $client = $this->database->table('users')->get($id);

        $form = new Nette\Application\UI\Form;

        $form->addText('email', 'Email:')
        ->addRule(Nette\Forms\Form::EMAIL,'Email is in the wrong format')
        ->setDefaultValue($client->username);


        $form->addText('nickname', 'Screen Name:')
        ->setRequired('Please pick a screen name')
        ->setDefaultValue($client->nickname);


        $form->addUpload('profile', 'Profile Picture:')
        ->addRule( Nette\Application\UI\Form::IMAGE, 'Profile must be JPEG, PNG or GIF');

        $form->addSubmit('send','Update Profile');

        // call method signInFormSucceeded() on success
	$form->onSuccess[] = $this->profileFormSucceeded;


        return $form;
	}

Last edited by kc2scy (2014-02-04 06:01)

llsm
Member | 121
+
0
-

try this without dot in the start of path (that makes path relative to index root, not your current location):

<?php
public function renderDefault()
    {
       $this->template->profilePic = '/images/'. $this->id. '/'.$this->client->image;

    }
?>

If it doesnt work, just find the correct path using your browser, this code should create path http://<your domain whatever it is>/images/<passed id>/<imageName>
Is image extension part of name? dont forget about extension…

Pavel Macháň
Member | 282
+
0
-

Or use some service for image path
Example:

class GalleryDirectories {

    /** @var string */
    private $original;

    /** @var string */
    private $thumbnail;


    /**
     * @param string $original
     * @param string $thumbnail
     */
    public function __construct($original, $thumbnail) {
        $this->original = $original;
        $this->thumbnail = $thumbnail;
    }

    /**
     * @param string $original
     * @return void
     */
    public function setOriginal($original) {
        $this->original = $original;
    }

    /**
     * @return string
     */
    public function getOriginal() {
        return $this->original;
    }

    /**
     * @param string $thumbnail
     * @return void
     */
    public function setThumbnail($thumbnail) {
        $this->thumbnail = $thumbnail;
    }

    /**
     * @return string
     */
    public function getThumbnail() {
        return $this->thumbnail;
    }

}

Config

parameters:
	gallery:
		thumbnail: %wwwDir%\gallery\thumbnail
		original: %wwwDir%\gallery\original
services:
	# Directories
	- GalleryDirectories(%gallery.original%, %gallery.thumbnail%)

Presenter

class HomepagePresenter extends BasePresenter {

    /**
     * @var \GalleryDirectories
     * @inject
     */
    public $galleryDirectories;


    public function renderDefault() {
        $this->template->profilePic = $this->galleryDirectories->getOriginal().'/imageName.png';
    }

}

Or latte version

public function renderDefault() {
    $this->template->directories = $this->galleryDirectories;
    $this->template->profilePic = 'imageName.png';
}
<img src="{$directories->getOriginal()}/{$profilePic}" alt="No Image" height="200" width="300">

OR use Nette object

class GalleryDirectories extends \Nette\Object { ... }
<img src="{$directories->original}/{$profilePic}" alt="No Image" height="200" width="300">

Last edited by EIFEL (2014-02-04 11:47)

kc2scy
Member | 22
+
0
-

llsm wrote:

try this without dot in the start of path (that makes path relative to index root, not your current location):

<?php
public function renderDefault()
    {
       $this->template->profilePic = '/images/'. $this->id. '/'.$this->client->image;

    }
?>

If it doesnt work, just find the correct path using your browser, this code should create path http://<your domain whatever it is>/images/<passed id>/<imageName>
Is image extension part of name? dont forget about extension…

What happing is, in my templete I'm getting a error saying the variable don't exist (“variable profilePic don't exsist”). I should have been more clear on what my issue was thanks again for looking

Last edited by kc2scy (2014-02-04 16:31)

kc2scy
Member | 22
+
0
-

Ok I figure it out,
I went back and took a look at the getting started app I created and it dawn on me that I didn't understand the presenter structure and naming convention. after some studying and playing around I have it working. Thanks all you for your help.

alvisandersonq
Member | 1
+
0
-

Thanks

baixue
Member | 3
+
-1
-

So if I want to display image in PHP like a star, then the algorithm?