question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Naming of the customized functions

See original GitHub issue

Hi Alex,

From #14,

  • the default renamer setup is StandardMemberRenamer which is basically transforming a camel/pascal case identifier to a lowercase+underscore identifier
  • you could also use context.MemberRenamer = new DelegateMemberRenamer(name => name); and it should just work without changing “Title” and “title”.
  • Note that this rename doesn’t affect the renamer used for all the builtins

My question is,

Suppose my C# customized function is named as MyCustomizedFunction, it will be available as mylib.my_customized_function normally, right? and if I’ve used context.MemberRenamer = new DelegateMemberRenamer(name => name), it will be available as-is as mylib.MyCustomizedFunction, right?

Thanks

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
xoofxcommented, Feb 22, 2017

This is a working sample:

using System;
using Scriban.Runtime;

namespace Scriban.Tests
{
    class Program
    {
        public static class MyLib
        {
            public static string UpCase(string text)
            {
                return text.ToUpperInvariant();
            }

            public static string DownCase(string text)
            {
                return text.ToLowerInvariant();
            }
        }

        static void Main(string[] args)
        {
            // Create a renamer as-is
            var renamer = new DelegateMemberRenamer(name => name);

            // Create an object that will contains these functions
            var myfunctions = new ScriptObject();
            myfunctions.Import(typeof(MyLib), ScriptMemberImportFlags.Method, null, renamer);

            // Prase the template
            var template = Template.Parse("This is UpCase {{ 'xXx' | UpCase }} and DownCase {{ 'YyY' | DownCase}}");


            var context = new TemplateContext { MemberRenamer = renamer }; // Use the same rename for member object access
            context.PushGlobal(myfunctions); // Use myfunctions as the current context
            // we could also push another object here to be the real nested context 
            template.Render(context);
            context.PopGlobal();

            Console.WriteLine(context.Output.ToString());
        }
    }
}

It is similar to how it is done for builtin functions:

  • you import some functions, properties, whatever to a ScriptObject
  • you push this ScriptObject as global context.

The difference for builtin functions is that they are created by default in the TemplateContext constructor and pushed (context.PushGlobal) as a global context… but you can have your nested context as well.

From the example above you could choose for example to expose these functions through a “myfunctions.DownCase” for example:

var obj = new ScriptObject();
obj.SetValue("myfunctions", myfunctions);
...
context.PushGlobal(obj)
0reactions
suntongcommented, Feb 22, 2017

Thank you so much!!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Naming guidelines for custom functions in Excel
Excel uses uppercase letters for built-in function names (such as SUM ). Use uppercase letters for your custom function's name and id as...
Read more >
Custom output function naming conventions
Follow these naming conventions for custom output functions: Write in camel case with the first letter in uppercase, for example DestinationFilePath().
Read more >
Defining a custom function
Note: This name must be unique for all custom functions in the current project. The name of the function is not case sensitive...
Read more >
Is there a naming convention for user defined function type?
Yes, adding the Func suffix to the type name is common and communicates its purpose well. Look at the standard lib for examples:....
Read more >
Lets talk naming conventions for Custom functions/modules
Lets talk naming conventions for Custom functions/modules. Hi. I am undertaking a small project to create custom functions and modules under ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found