Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Section Block Context in Helper #393

Closed
kchapple opened this issue Mar 1, 2022 · 2 comments
Closed

Section Block Context in Helper #393

kchapple opened this issue Mar 1, 2022 · 2 comments

Comments

@kchapple
Copy link

kchapple commented Mar 1, 2022

Greetings @bobthecow ! We are an open source project (OpenEMR) trying to port a set of templates from Ruby to PHP.

The problem is that somehow in the Ruby code, the object returned by patient_characteristic_birthdate is being pushed onto the context of the block, so that the helper function birth_date_time has access to the model's data directly. The birth_date_time helper seems to magically have access to self['birthDatetime'] when it's called from within the partial, which is a property on the Person model. Self is an instance of PersonCharacteristicBirthDate returned by the Lambda.

I don't know how to make that work in PHP. I just have access to the top level context. In PHP can instantiate an entirely new mustache engine instance in the Lambda and create an entirely new context, but I was wondering if there's a proper way to implement this.

Here is the Ruby I'm trying to emulate: Full code is in this repo: https://github.com/projecttacoma/cqm-reports/tree/master/lib/qrda-export/catI-r5

Template/Partial:

{{#patient_characteristic_birthdate}}
  {{{birth_date_time}}}
{{/patient_characteristic_birthdate}}

Helper:

def birth_date_time
  "<birthTime #{value_or_null_flavor(self['birthDatetime'])}/>"
end

Lambda:

def patient_characteristic_birthdate
   // Returns an array of PatientCharacteristicBirthDate objects which the section's helper to be immediately available
  JSON.parse(@Patient.get_data_elements('patient_characteristic', 'birthdate').to_json)
end
@kchapple
Copy link
Author

kchapple commented Mar 2, 2022

Here's what I had to do for the lambda to make the proper context available to the helper within the section block, because the template (which we don't really want to change) is expecting to have the context returned by patient_characteristic_birthdate() available. Is there any way to allow the helpers within a section block have access to only the context that the Lambda returns without building an entirely new view?

    public function patient_characteristic_birthdate()
    {
        $birthDate = $this->patient->get_data_elements('patient_characteristic', 'birthdate');
        return function ($text, $context) use ($birthDate) {
            $mustache = new \Mustache_Engine([
                'entity_flags' => ENT_QUOTES,
                'helpers' => [
                    'value_or_null_flavor' => function ($text) {
                        if (!empty($text)) {
                            $v = "value='{$text}'";
                        } else {
                            $v = "nullFlavor='UNK'";
                        }
                        return $v;
                    },
                    'birth_date_time' => function () use ($birthDate) {
                        $birth_date_time = $birthDate->birthDatetime->date;
                        $birth_date_time = date('Ymd', strtotime($birth_date_time));
                        return "<birthTime {{#value_or_null_flavor}}" . $birth_date_time . "{{/value_or_null_flavor}}/>";
                    }
                ]
            ]);
            return $mustache->render($text, $birthDate);
        };
    }

@kchapple
Copy link
Author

kchapple commented Mar 4, 2022

We found a workaround for this by not using helpers, and modifying the engine to pass the context to lambdas.

@kchapple kchapple closed this as completed Mar 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant