Tim Habersack

Where I put my things..

Laravel; having a conditional choose your @extends template

Mar 18th 2014

I am firmly entrenched in the Laravel camp now. One thing I love about Laravel is their blade templating language. However, there is some quirkiness that isn't immediately obvious.

Let's say you have a template that is extending another template. You want a conditional to determine which template you want to extend. (The use case here for me is a messaging system. Logic + layout is the same on back-end and front-end, but they extend different templates).

I was trying to get this to work:

@if($var)
@extends('account.base')
@else
@extends('base.base')
@endif

@section('content')

<h1>Here are things!</h1>

@stop

All it was doing was outputting the one @extends('base.base') line. After much hunting, I found this great answer on StackOverflow. So, @extends() must be on the first line of a blade template, which is a little weird, but whatever. Used ternary operator like this and it works!

@extends($var ? 'account.base' : 'base.base')

It's not the people you miss, it's the moment.

Jan 20th 2014

It's not the people you miss, it's what you felt during those moments. And those feelings can't really be felt again. It was for a younger you. Focus on where you are now, don't try to recreate the past.

Time for Reflection

Jan 16th 2014

My family just got back from our first vacation in over three years. After us getting married and having two wonderful children it was definitely overdue.

I have had a lot of small revelations about things that I am still processing. As I iron out the details I will be sharing them. Expect posts aplenty!

Dec 2nd 2013

Just finished the todo list for the next four days, after which we launch a new project that is running on Station, the new CMS package for Laravel we developed. It's going to be intense, and worth it!

Nov 26th 2013

I found this great PHP input validation class called GUMP. It is only ~32Kb, one file. Looks perfect to integrate with Fat Free Framework.

Nov 25th 2013

Test status update from my phone.

title one

Nov 25th 2013

I've been wearing my sysadmin hat all morning and I like it. Love spinning up and customizing new instances.

Crazy amounts of new knowledge

Sep 26th 2013

A couple days ago I passed my 5 month mark at my new job as a Senior Developer at the Canary Collective. It's been really great working with some super talented people like Ben, Blake and Todd. But also, holy crap the new stuff I've been learning!

There are more I just can't think of them at the moment. Point is, getting to learn a lot which is awesome. Also, dealing with a lot more front-end work that I was used to. It is pushing me out of my comfort-bubble which is a good thing.

Moving options between select boxes

Feb 8th 2013

Just a quick post. I needed to be able to display a multi-select box showing all the options available to a user, and allow the user to select one or several, and 'move' them to their list of chosen options. Then, when the form is submitted, make sure only the items in the chosen list of options is submitted.

Fairly easy right? It was, but took long enough that I wanted to share and tag appropriately, in case people come looking for it. :)

Image of what I wanted: Multiselect image

Link to my working JSFiddle.

HTML:


Javascript:

$('.go_in').click(function() {
    return !$('.all_options option:selected').remove().appendTo('.chosen_options');
});
$('.go_out').click(function() {
   return !$('.chosen_options option:selected').remove().appendTo('.all_options'); 
});

$('form').submit(function() {
    $('.all_options option').prop('selected','');
    $('.chosen_options option').prop('selected','selected');
    alert($(this).serialize());
});

Victory over a big wall

Jan 24th 2013

Progress on Lemon-filling has been going well, though I hit a big wall a couple of days ago. I was having problems getting the exact results I wanted with my query.

The problem was, when requesting a pages terms, I wanted all the terms and their definitions, like this if asking for locale_id=1:

+---------------+------------------------------------------+
| terms.value   | rosetta.value                            |
+---------------+------------------------------------------+
| lname         | Username:                                |
| lpass         | Password:                                |
| welcome_blurb | Welcome to appland! Please log in below. |
+---------------+------------------------------------------+

If I requested a locale that existed, but didn't have some or any of the terms defined yet, I wanted this:

+---------------+---------------+
| terms.value   | rosetta.value |
+---------------+---------------+
| lname         |               |
| lpass         |               |
| welcome_blurb |               |
+---------------+---------------+

But I was just getting an empty result set. Spent a while agonizing over it, eventually took the time to write up my problem and posted it on dba.stackexchange.com. Within minutes help arrived!

It pointed me in the right direction, but the solution offered:

SET @given_locale_id = 1;
SELECT terms_value,MAX(rosetta_value) rosetta_value
FROM
(
    SELECT T.value terms_value,R.value rosetta_value,R.locale_id
    FROM terms T LEFT JOIN rosetta R
    ON T.terms_id=R.terms_id
    UNION
    SELECT T.value,'',@given_locale_id FROM terms T
) A
WHERE
    locale_id = @given_locale_id
GROUP By terms_value;

Only worked when asking for all terms. I also need a query where I give it the page key, and it only gives be back the terms for that page. Tinkered a lot, and eventually came up with a solution:

SET @given_locale_id = 1;
SELECT terms_value, MAX( rosetta_value ) rosetta_value
FROM (
 
    SELECT T.value terms_value, T.terms_id, R.value rosetta_value, R.locale_id
    FROM terms T
    LEFT JOIN rosetta R ON T.terms_id = R.terms_id
    UNION
    SELECT T.value, T.terms_id, '', @given_locale_id
    FROM terms T        
 
)A
WHERE locale_id =@given_locale_id AND terms_id IN (SELECT terms_id FROM page_group INNER JOIN page ON page.page_id=page_group.page_id WHERE page.value='sign_in')
GROUP BY terms_value;

It is one of the most complex queries I've had to write. I will probably need to optimize some too, but I'm waiting until I know for sure that my schema design will work before putting in foreign key constraints, which should help.

< Older Newer >