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

Wrap + Include #452

Open
Arthri opened this issue Jul 17, 2022 · 7 comments
Open

Wrap + Include #452

Arthri opened this issue Jul 17, 2022 · 7 comments
Labels

Comments

@Arthri
Copy link
Contributor

Arthri commented Jul 17, 2022

How to combine wrap and include?

{{ wrap include "Template" "args" }}
content
{{ end }}

Example template. It is not possible to obtain $$ inside Template here

@xoofx
Copy link
Member

xoofx commented Jul 29, 2022

Wrap takes a function reference, include returns a text, so it's not a function reference.

@xoofx xoofx added the question label Jul 29, 2022
@Arthri
Copy link
Contributor Author

Arthri commented Jul 30, 2022

I see. Is it possible to some block-level text to an include function? Thanks for your reply!

@xoofx
Copy link
Member

xoofx commented Jul 30, 2022

Is it possible to some block-level text to an include function?

Sorry, I don't understand this question. Could you elaborate?

@Arthri
Copy link
Contributor Author

Arthri commented Jul 30, 2022

I have two templates
main.sbntxt

{{ include partial.sbntxt "Placeholder" }}
{%{ Pass the following text to function }%}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam a nibh non sem vehicula blandit. Cras ornare eget ex vitae tempus. Nullam sit amet iaculis metus. Mauris dapibus id metus at congue. Aliquam mattis sollicitudin nibh eu efficitur. Aenean non rhoncus ex. Nullam porta ante vel consequat scelerisque. Sed in tellus id metus varius posuere vel quis augue. Nam fermentum erat est, lobortis pellentesque enim fermentum et. Morbi nibh lorem, scelerisque ut odio eu, pretium bibendum tortor.

partial.sbntxt

# {{ $1 }}
{{ $$ }}

and I want it to become something like

# Placeholder
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam a nibh non sem vehicula blandit. Cras ornare eget ex vitae tempus. Nullam sit amet iaculis metus. Mauris dapibus id metus at congue. Aliquam mattis sollicitudin nibh eu efficitur. Aenean non rhoncus ex. Nullam porta ante vel consequat scelerisque. Sed in tellus id metus varius posuere vel quis augue. Nam fermentum erat est, lobortis pellentesque enim fermentum et. Morbi nibh lorem, scelerisque ut odio eu, pretium bibendum tortor.

@xoofx
Copy link
Member

xoofx commented Jul 30, 2022

So warp takes a reference to a function so I would believe that using an anonymous function could work in your case:

{{ wrap @(do; include "Template" "args"; end) }}
content
{{ end }}

@Arthri
Copy link
Contributor Author

Arthri commented Jul 31, 2022

That works perfectly! Thank you!

@Arthri Arthri closed this as completed Jul 31, 2022
@Arthri
Copy link
Contributor Author

Arthri commented Jul 24, 2024

Wrap takes a function reference, include returns a text, so it's not a function reference.

Things may have changed since I made this discussion, but as far as I can tell, wrap does take include as a function reference in this case. However, $$ is only set by default in functions defined inside Scriban, functions imported into Scriban must manually define it themselves.

I've found that the following implementation works for me

internal class IncludeW : IScriptCustomFunction
{
    private readonly IncludeFunction _function = new();

    public int RequiredParameterCount => _function.RequiredParameterCount;

    public int ParameterCount => _function.ParameterCount;

    public ScriptVarParamKind VarParamKind => _function.VarParamKind;

    public Type ReturnType => _function.ReturnType;

    public ScriptParameterInfo GetParameterInfo(int index)
    {
        return _function.GetParameterInfo(index);
    }

    public object Invoke(TemplateContext context, ScriptNode callerContext, ScriptArray arguments, ScriptBlockStatement blockStatement)
    {
        context.SetValue(ScriptVariable.BlockDelegate, blockStatement);
        return _function.Invoke(context, callerContext, arguments, blockStatement);
    }

    public async ValueTask<object> InvokeAsync(TemplateContext context, ScriptNode callerContext, ScriptArray arguments, ScriptBlockStatement blockStatement)
    {
        await context.SetValueAsync(ScriptVariable.BlockDelegate, blockStatement);
        return await _function.InvokeAsync(context, callerContext, arguments, blockStatement);
    }
}

Used like so

/{{ $$ }}/
{{ wrap includew 'template' -}}
nested text
{{- end }}

resulting in

/nested text/

@Arthri Arthri reopened this Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants