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

Feat/templating #1142

Merged
merged 23 commits into from
Nov 27, 2023
Merged

Feat/templating #1142

merged 23 commits into from
Nov 27, 2023

Conversation

ziggy-cyb
Copy link
Collaborator

Description

Sometimes if there's a lot of variable content in a form, setting conditions for each bit of content can become cumbersome and in serious cases can cause the form runner to crash. Using templating we can allow conditional content to be populated based on previous user input without the need for conditions.

  • Added additionalContexts.json to load in the context
  • Updated views plugin to add the contexts to the global nunjucks context
  • Added helper functions to grab relevant fields from the form state to add to the component context
  • Updated HTML component to use new helper functions

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce
the testing if necessary.

  • e2e test using example form to check content is coming through properly
  • Manual testing with various other cases to check templated variables are coming through as they should

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation and versioning
  • I have updated the architecture diagrams as per Contribute.md OR added an architectural decision record entry

@@ -90,3 +91,34 @@ export const addClassOptionIfNone = (
options.classes = className;
}
};

export function getVarsFromContent(content: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should probably just use njks and renderString in this case.

We should also add an environment variable ALLOW_USER_TEMPLATES.

If true - we'll use njks and render. It looks like it could be pretty easy to inject script tags otherwise

@ziggy-cyb ziggy-cyb force-pushed the feat/templating branch 2 times, most recently from 711dabb to 3d80617 Compare November 23, 2023 11:45
@@ -90,3 +92,33 @@ export const addClassOptionIfNone = (
options.classes = className;
}
};

export function getVarsForTemplate(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused?

@@ -420,8 +440,7 @@ export class PageControllerBase {
}
});
}

const viewModel = this.getViewModel(formData, num);
const viewModel = this.getViewModel(formData, num, undefined, state);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does state need to be passed in?

@@ -88,14 +88,15 @@ export class ComponentCollection {
getViewModel(
formData: FormData | FormSubmissionState,
errors?: FormSubmissionErrors,
conditions?: FormModel["conditions"]
conditions?: FormModel["conditions"],
state?: FormSubmissionState
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we still need state, now that formData should have it?

@ziggy-cyb ziggy-cyb merged commit d443ec2 into main Nov 27, 2023
12 checks passed
@ziggy-cyb ziggy-cyb deleted the feat/templating branch November 27, 2023 15:15
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

Successfully merging this pull request may close these issues.

2 participants