Forum

Thread tagged as: Question, Error, Api

Passing variables into templates with the API

Hello,

I'm not sure if I'm missing something or doing something wrong but I need to access the ID of an item from within a template with my custom app.

What I'm doing is displaying a fixed field

echo $Form->text_field('projectName', 'Project Name', isset($details['projectName'])?$details['projectName']:false, false, false);

Then displaying the template fields

echo $Form->fields_from_template($Template, $details, $Projects->static_fields);

Within that template i have a repeater:

<perch:repeater id="layout" label="Layout" divider-before="Layout" scope-parent="true">

    <perch:projects id="device" label="Device" type="devices" project="<perch:projects id="parent.projectID" />" />

</perch:repeater>

The device is a custom field type i have built that looks up devices relating to the current project, but I can't work out how to pass the current project id into the field.

Dexter Harrison

Dexter Harrison 29 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

What is parent.projectID ? How is it populated before the repeater is rendered? I don't think this makes sense.

Well parent.projectID i tried because just projectID didn't work. projectID is the actual id of the item. So would assume it should be accessible, but I'm assuming the template rendering doesn't work like that in the interface?

I've tried it outside the repeater too it doesn't work.

Debug within the interface does this:

SELECT * FROM perch2_dh_projects_devices WHERE projectID =<perch:projects id=

I basically just need a way to pass the current item id for use within the field type.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What defines a project as current when you're editing the item?

Query string like normal so,

$projectID = (int) $_GET['id'];    
$Project = $Projects->find($projectID, true);
Drew McLellan

Drew McLellan 2638 points
Perch Support

That's not going to be the case in an edit form though, is it?

I don't really understand what you mean, everything is working so I find the item can change all the details and then update the record with $Project->update($data) all that works with the dynamic fields. But within the template of the edit form I can't access the Project ID.

I don't know I might be asking for something that isn't possible, although accessing the id of item from the template I would of thought could be done.

I guess what you're saying is when you define a template with

$Template->set('template.html', 'projects');

There is no indication what the project is that you're editing from within the template.

Duncan Revell

Duncan Revell 78 points
Registered Developer

When do you actually need to have the $projectID - a) when adding/editing data in the admin console, or b) when outputting data to your web page?

$Template->set() is used with b) - when getting data form the database and outputting it to your web page via a template. You can use PerchSystem::set_var() before this.

If a), surely that would require you saving the record before adding a repeater item, in order to be able to somehow "call" the projectID inside the repeater? If this one, does your custom fieldtype present the admin with a list of devices that only relate to the record you're editing in the admin console?

Is one of those what you're trying to achieve?

It's a, I need it in the admin side. This particular set of content doesn't touch the webpage.

Yeah the record is saved prior to this template being shown, it's on a tab that's displayed when editing an existing record.

Basically I have two tables a table for devices and a table for projects, you can assign a device to a project then I have a field type that finds all devices related to the project. But the way I have done it (Maybe there is a better way) is the custom field type that lists all devices relating to that project (attribute project="projectID" so I can use it within a repeater.

I'll have a think of a different way I can achieve what I'm trying to do.

Only way I can think of doing it is within the field type define the projectID with $_GET['id'] not ideal, but it works.

Duncan Revell

Duncan Revell 78 points
Registered Developer

I was about to suggest the same - the attribute in the template is not useable unfortunately. I guess at least you know the fieldtype will always be used where id exists in the url...

Thanks for your help Duncan, I'll give you the point for your contribution.