[racket-users] Bending use of include-template to my needs (or, macro results as macro arguments)

2019-02-19 Thread Brian Adkins
I would like to take the following web request handler:

(define (login request)
  (render-string
   (include-template "../views/authentication/login.html")))

and eventually get to something like:

(define (login request)
  (render login))

My current lack of understanding with respect to macros is limiting me :) 
For now, I'd be content with the following:

(define (login request)
  (render-string
   (include-template (view login

which I tried to get to work with this macro:

(define-syntax-rule (view name)
  (string-append "../views/authentication/" (symbol->string (quote name)) 
".html"))

But, I'm guessing that the include-template macro is unable to consume the 
output of my view macro. Is there anything I can do to get (view login) to 
be expanded prior to when include-template needs it?

Thanks,
Brian


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Bending use of include-template to my needs (or, macro results as macro arguments)

2019-02-19 Thread Matthew Butterick

> On Feb 19, 2019, at 8:25 AM, Brian Adkins  wrote:
> 
> But, I'm guessing that the include-template macro is unable to consume the 
> output of my view macro. Is there anything I can do to get (view login) to be 
> expanded prior to when include-template needs it?

Wrap `include-template` with your own macro that generates the string you want?

#lang racket
(require web-server/templates)

(define-syntax (include-template/named stx)
  (syntax-case stx ()
[(_ NAME)
 (with-syntax ([STR (string-append "../views/authentication/" 
(symbol->string (syntax-e #'NAME)) ".html")]) 
   #'(include-template STR))]))

(define (login request)
  (include-template/named login))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Bending use of include-template to my needs (or, macro results as macro arguments)

2019-02-19 Thread Brian Adkins
On Tuesday, February 19, 2019 at 1:45:43 PM UTC-5, Matthew Butterick wrote:
>
>
> On Feb 19, 2019, at 8:25 AM, Brian Adkins  > wrote:
>
> But, I'm guessing that the include-template macro is unable to consume the 
> output of my view macro. Is there anything I can do to get (view login) to 
> be expanded prior to when include-template needs it?
>
>
> Wrap `include-template` with your own macro that generates the string you 
> want?
>
> #lang racket
> (require web-server/templates)
>
> (define-syntax (include-template/named stx)
>   (syntax-case stx ()
> [(_ NAME)
>  (with-syntax ([STR (string-append "../views/authentication/" 
> (symbol->string (syntax-e #'NAME)) ".html")]) 
>#'(include-template STR))]))
>
> (define (login request)
>   (include-template/named login))
>
>
That's perfect Matthew - thanks! 

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Bending use of include-template to my needs (or, macro results as macro arguments)

2019-02-19 Thread Brian Adkins


On Tuesday, February 19, 2019 at 4:03:02 PM UTC-5, Brian Adkins wrote:
>
> On Tuesday, February 19, 2019 at 1:45:43 PM UTC-5, Matthew Butterick wrote:
>>
>>
>> On Feb 19, 2019, at 8:25 AM, Brian Adkins  wrote:
>>
>> But, I'm guessing that the include-template macro is unable to consume 
>> the output of my view macro. Is there anything I can do to get (view login) 
>> to be expanded prior to when include-template needs it?
>>
>>
>> Wrap `include-template` with your own macro that generates the string you 
>> want?
>>
>> #lang racket
>> (require web-server/templates)
>>
>> (define-syntax (include-template/named stx)
>>   (syntax-case stx ()
>> [(_ NAME)
>>  (with-syntax ([STR (string-append "../views/authentication/" 
>> (symbol->string (syntax-e #'NAME)) ".html")]) 
>>#'(include-template STR))]))
>>
>> (define (login request)
>>   (include-template/named login))
>>
>>
> That's perfect Matthew - thanks! 
>

Oops - I spoke too soon. It appears the lexical context is unavailable to 
the template when include-template is used in this manner. 

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Bending use of include-template to my needs (or, macro results as macro arguments)

2019-02-19 Thread Matthew Butterick


> On Feb 19, 2019, at 1:28 PM, Brian Adkins  wrote:
> 
> Oops - I spoke too soon. It appears the lexical context is unavailable to the 
> template when include-template is used in this manner. 


Ah right, one has to inject STR into the same lexical context, like so:


#lang racket
(require web-server/templates)

(define-syntax (include-template/named stx)
  (syntax-case stx ()
[(_ NAME)
 (with-syntax ([STR (datum->syntax #'NAME (string-append 
"../views/authentication/" (symbol->string (syntax-e #'NAME)) ".html"))]) 
   #'(include-template STR))]))

(define (login request)
  (include-template/named login))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Bending use of include-template to my needs (or, macro results as macro arguments)

2019-02-19 Thread Brian Adkins
On Tuesday, February 19, 2019 at 5:06:32 PM UTC-5, Matthew Butterick wrote:
>
>
>
> On Feb 19, 2019, at 1:28 PM, Brian Adkins  > wrote:
>
> Oops - I spoke too soon. It appears the lexical context is unavailable to 
> the template when include-template is used in this manner. 
>
>
> Ah right, one has to inject STR into the same lexical context, like so:
>
>
> #lang racket
> (require web-server/templates)
>
> (define-syntax (include-template/named stx)
>   (syntax-case stx ()
> [(_ NAME)
>  (with-syntax ([STR (datum->syntax #'NAME (string-append 
> "../views/authentication/" (symbol->string (syntax-e #'NAME)) ".html"))]) 
>#'(include-template STR))]))
>
> (define (login request)
>   (include-template/named login))
>


Awesome - that did the trick. I'm confused as to why it helped to inject 
STR into the lexical context - it ends up being just a string, and it was 
working fine with respect to loading the template. I would think the fix 
would be to inject include-template into the lexical context. I suppose it 
must have something to do with how the include-template macro is dealing 
with that argument. 

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Bending use of include-template to my needs (or, macro results as macro arguments)

2019-02-19 Thread Matthew Butterick

> On Feb 19, 2019, at 2:33 PM, Brian Adkins  wrote:
> 
> Awesome - that did the trick. I'm confused as to why it helped to inject STR 
> into the lexical context - it ends up being just a string, and it was working 
> fine with respect to loading the template. I would think the fix would be to 
> inject include-template into the lexical context. I suppose it must have 
> something to do with how the include-template macro is dealing with that 
> argument. 

When `with-syntax` finds a datum (like a string) on the right side of a 
pattern-binding expression, it silently coerces it to a syntax object with the 
macro-definition scope attached (which in this case, was not the right scope).

Ordinarily you wouldn't want to move `include-template` into the lexical 
context of the calling site, because you're relying on retaining the binding it 
has at the macro-definition site.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.