Fork me on GitHub
#beginners
<
2017-02-05
>
eslachance01:02:38

I'm going to go ahead and make a folder in my bookmark bar 😄

grounded_sage11:02:35

How can I start a clojure atom off with a system property

roelof12:02:21

How can I make the if then it a let work. I have now this :

(let [prevnext (if (< page 6) (- 11 page) else 6)
      num-pages 466
      page 466
      lower (max 1 (- page prevnext))
      upper (inc (min num-pages (+ page prevnext)))]
  (range lower upper))  

roelof12:02:19

and I see a message that if has to many arguments

rads12:02:34

the syntax is (if <condition> <true-expr> <false-expr>)

rads12:02:48

so (if (< page 6) (- 11 page) 6) is probably what you want

rads12:02:34

that's assuming page is defined earlier somewhere else in the code. otherwise you will get an error since page in the snippet above is defined after prevnext. the order is important with the let bindings

roelof12:02:45

Thanks, I saw that error and solved it

roelof12:02:54

so everything seems to work well

roelof14:02:31

I have this function :

(ns paintings2.pagination)

(def pagination-numbers
  []
 (let [page (data.current-page)
       prevnext (if (< page 6) (- 11 page) (if (> page 460 ) (- 11 (- 467 page)) 5))
       num-pages 475
       lower (max 1 (- page prevnext))
       upper (inc (min num-pages (+ page prevnext)))]
  (range lower upper)))

roelof14:02:11

now I use it like this :

{% for number in (paintings2.pagination/pagination-numbers()) %}

roelof14:02:36

but now I see no output . it looks like the function returns nothing

roelof14:02:54

Any tips how to solve this ?

roelof15:02:31

the last line is in a Selmer template

dpsutton15:02:04

can you call functions like that in selmer?

madstap16:02:48

@roelof You need to call the funtction in the clojure code that renders the selmer template. Check out the link I posted.

madstap16:02:10

(render "{% for number in pagination-numbers %}" {:pagination-numbers (paintings2.pagination/pagination-numbers)})

roelof17:02:16

@madstap thanks, I have some rwriting to do then

roelof17:02:38

@madstap : one question. How can I make it work that in the for number loop I made a <li>

roelof17:02:00

I think I can place the code you posted into the template ?

madstap17:02:24

@roelof Not really sure what you're asking... You want something like this <ul>{% for number in pagination-numbers %}<li>{{number}}</li>{% endfor %}</ul>, right?

roelof17:02:51

no, I want {% for .... } <li> ,,,, </li> <% end for %}

roelof17:02:55

Can I do something like this :

(render "{% for number in pagination-numbers %} <li> ....  </li>" {:pagination-numbers (paintings2.pagination/pagination-numbers)}) 

dominicm17:02:19

You can, but you need an {% endfor %} at the end of the string too

roelof17:02:40

oke, both thanks, I will try that

roelof17:02:50

nope, when I place this :

 (render "{% for number in pagination-numbers %} <li><a href="#"></a> number </li> {% endfor %}" {:pagination-numbers (paintings2.pagination/pagination-numbers)}) 
I see the code in my website not the numbers 😞

roelof17:02:04

I see now this in my html :  (render "" {:pagination-numbers (paintings2.pagination/pagination-numbers)})

roelof17:02:26

or can I not place this in my base.html file

madstap17:02:43

You need to put it in the .clj file

roelof17:02:50

bummer, I think I need to rethink the way I achieve my goal.

roelof17:02:38

I want to display number which stands for pagenumbers. These are generated by the pagination-numbers function

roelof17:02:07

in my base.html file I have a footer part where I want to display those numbers

madstap17:02:13

How does the code where you render base.html look?

madstap17:02:22

Should be something like:

roelof17:02:34

I have this home.html page :

{% extends "base.html" %}

{% block content %}

 {% for painting in data.paintings %}

 <div class="post-masonry col-md-4 col-sm-6">
    <div class="post-thumb">
        <img src= {{ painting.tiles}} alt="">
        <div class="title-over">
            <h4><a href="#"> {{painting.title}} </a></h4>
        </div>
        <div class="post-hover text-center">
            <div class="inside">
                <i class="fa fa-plus"></i>
                <span class="date">25 Jan 2084</span>
                <h4><a href="#">Title one goes here</a></h4>
                <p>Cum sociis natoque penatibus et magnis dis parturient</p>
            </div>
        </div>
    </div>
</div> <!-- /.post-masonry -->

 {% endfor %}

{% endblock %}
`

roelof17:02:23

which responds to this route :

(layout/render
      "home.html" {:data {:paintings (-> (client/get url options)
                                      (api/get-objectNumbers)
                                      (api/fetch-paintings-and-images-front-page))
                          :current-page page-num}})))

madstap17:02:03

Right, this is where you need to add the :pagination-numbers key

roelof17:02:55

oke, so I f I add a pagination-numbers key in home.html , base.html can use it

roelof17:02:26

oke, I will try it that way after dinner

roelof17:02:37

I thought base.html would not find it

roelof17:02:31

and in base,html I can do

{%  for number in data.pagination-number  %}  

roelof17:02:58

thanks, I will try it after dinner

madstap18:02:24

If :pagination-numbers is a sequence of numbers

roelof18:02:53

yep, it returns a range of numbers

roelof18:02:46

one problem.

roelof18:02:56

I have this part :

"home.html" {:data {:paintings (-> (client/get url options)
                                      (api/get-objectNumbers)
                                      (api/fetch-paintings-and-images-front-page))
                       :current-page page-num
                       :pagination-numbers (pagination/pagination-numbers)}}))

roelof18:02:15

which calls this function :

(ns paintings2.pagination)

(def pagination-numbers
  []
 (let [page (data.current-page)
       prevnext (if (< page 6) (- 11 page) (if (> page 460 ) (- 11 (- 467 page)) 5))
       num-pages 475
       lower (max 1 (- page prevnext))
       upper (inc (min num-pages (+ page prevnext)))]
  (range lower upper)))

roelof18:02:44

but now I see this error message :

Too many arguments to def, compiling:(paintings2/pagination.clj:3:1) 

rads18:02:51

you need defn instead of def

roelof18:02:17

I have this in my require :

[paintings2.pagination :as pagination]  

roelof18:02:41

and see this error :

namespace 'paintings2.pagination'  

rads18:02:23

what does your full ns statement look like?

roelof18:02:46

(ns paintings2.routes.home
  (:require [paintings2.layout :as layout]
            [compojure.core :refer [defroutes GET]]
            [ring.util.http-response :as response]
            [ :as io]
            [compojure.route :refer [resources]]
            [environ.core :refer [env]]
            [paintings2.api-get :as api]
            [paintings2.pagination :as pagination]
            [clj-http.client :as client]
            [clojure.spec :as s]))

roelof18:02:21

and of the pagination :

(ns paintings2.pagination) 

rads18:02:31

ok, and what's the full error you're getting?

roelof18:02:55

java.lang.Exception: namespace 'paintings2.pagination' not found, compiling:(paintings2/routes/home.clj:1:1) 

roelof18:02:09

java.lang.Exception: namespace 'paintings2.pagination' not found, compiling:(paintings2/routes/home.clj:1:1)
        at clojure.core$throw_if.invokeStatic(core.clj:5771)
        at clojure.core$load_lib.invokeStatic(core.clj:5857)
        at clojure.core$load_lib.doInvoke(core.clj:5832)
        at clojure.lang.RestFn.applyTo(RestFn.java:142)
        at clojure.core$apply.invokeStatic(core.clj:659)

        at clojure.core$load_libs.invokeStatic(core.clj:5889)
        at clojure.core$load_libs.doInvoke(core.clj:5873)
        at clojure.lang.RestFn.applyTo(RestFn.java:137)
        at clojure.core$apply.invokeStatic(core.clj:659)
        at clojure.core$require.invokeStatic(core.clj:5911)
        at clojure.core$require.doInvoke(core.clj:5911)
        at clojure.lang.RestFn.invoke(RestFn.java:805)
        at paintings2.routes.home$eval17751$loading__7732__auto____17752.invoke(home.clj:1)
        at paintings2.routes.home$eval17751.invokeStatic(home.clj:1)
        at paintings2.routes.home$eval17751.invoke(home.clj:1)
        at clojure.lang.Compiler.eval(Compiler.java:6978)
        at clojure.lang.Compiler.eval(Compiler.java:6967)
        at clojure.lang.Compiler.load(Compiler.java:7430)

rads18:02:18

my guess is that paintings2/pagination.clj has an error somewhere

rads18:02:35

the expression (data.current-page) won't compile

roelof18:02:26

oke, that comes from here :

(defn home-page [page]
  (let [page-num (s/conform ::page page)
        url ""
        options {:as :json :query-params {:key (env :key) :format "json" :type "schilderij" :toppieces "True" :p page-num :ps 10}}]
    (if (s/invalid? page-num) 1 page-num)
    (layout/render)
   "home.html" {:data {:paintings (-> (client/get url options)
                                      (api/get-objectNumbers)
                                      (api/fetch-paintings-and-images-front-page))
                       :current-page page-num
                       :pagination-numbers (pagination/pagination-numbers)}}))

rads18:02:21

pass in page-num as an argument instead, e.g. (pagination/pagination-numbers page-num)

rads18:02:48

data.current-page is referring to a namespace that doesn't exist in your project

rads18:02:37

furthermore, even if it were being used as a namespace, it's not a valid var because it's missing the symbol, i.e. data.current-page/my-var

roelof18:02:51

if I understand you right, delete the current-page part and make it a parameter of the pagination-numbers function ?

rads18:02:15

you can delete :current-page page-num if it's not being used elsewhere

rads18:02:27

that's correct

rads18:02:12

in clojure when you have a function that needs data from somewhere else, it's usually best to just pass in the data as an argument

roelof18:02:31

#rads. I have now this :

(defn home-page [page]
  (let [page-num (s/conform ::page page)
        url ""
        options {:as :json :query-params {:key (env :key) :format "json" :type "schilderij" :toppieces "True" :p page-num :ps 10}}]
    (if (s/invalid? page-num) 1 page-num)
    (layout/render)
   "home.html" {:data {:paintings (-> (client/get url options)
                                      (api/get-objectNumbers)
                                      (api/fetch-paintings-and-images-front-page))
                       :pagination-numbers (pagination/pagination-numbers page-num)}}))
and
(ns paintings2.pagination)

(defn pagination-numbers
  [page]
 (let [prevnext (if (< page 6) (- 11 page) (if (> page 460 ) (- 11 (- 467 page)) 5))
       num-pages 475
       lower (max 1 (- page prevnext))
       upper (inc (min num-pages (+ page prevnext)))]
  (range lower upper)))

roelof18:02:39

but still the same error

roelof18:02:20

it looks like to me there is something wrong in the require part

rads18:02:33

if you can run pagination-numbers from a REPL then you know the problem is in home.clj

rads18:02:12

the paintings2.pagination code works for me so it's probably in home.clj

rads18:02:23

the ns statement you pasted earlier looks fine to me

rads18:02:18

can you do (require 'paintings2.pagination) in a REPL?

swizzard19:02:12

lein is giving me a very weird error when i try to do anything:

Could not find artifact base:lein-template:jar:0.1.0-SNAPSHOT in clojars ()
This could be due to a typo in :dependencies or network issues.
If you are behind a proxy, try setting the 'http_proxy' environment variable.
i’m not behind a proxy, and there aren’t any network issues as far as i can tell

swizzard19:02:57

i just reinstalled lein just to be on the safe side

swizzard19:02:31

upon further investigation, i can’t even seem to ping

roelof19:02:45

@rads When I do it on a repl I get as output nil so I seems to work well

roelof19:02:10

when I do

[paintings2.pagination :as pag] 
everything works fine

rads19:02:47

and if you change it back it gives you the error again?

roelof19:02:12

very wierd, everything still works

roelof19:02:25

but I see another errormessage

roelof19:02:35

I have this :

(defn pagination-numbers
  [page]
 (let [prevnext (if (< page 6) (- 11 page) (if (> page 460 ) (- 11 (- 467 page)) 5))
       num-pages 475
       lower (max 1 (- page prevnext))
       upper (inc (min num-pages (+ page prevnext)))]
  (range lower upper)))

roelof19:02:36

and on the prevnext line I see this error message :

clojure.lang.Keyword cannot be cast to java.lang.Number  

roelof19:02:10

Here I test if the page is a valid number :

[page-num (s/conform ::page page) 

roelof19:02:37

here it's a 1 or the orginal number :

(if (s/invalid? page-num) 1 page-num) 

roelof19:02:56

so why is page-num suddenly a keyword ???

roelof19:02:27

anyone a idea ?

roelof19:02:14

When I do the pagination-numbers in repl then there is no problem ,

(pagination-numbers 5)  
gives a output

dominicm19:02:06

@roelof putting in (println page) above your let might give you a clue

dominicm19:02:38

You can also put a binding to _ in your let and print out page-num

dominicm19:02:21

e.g.

(let [page-num (s/conform ::page page)
      _ (println page-num)
      other (bindings)]
  …)

roelof19:02:00

@dominicm I have a clue. When I do that in the pagination function page seems to have this as content : :clojure.spec/invalid

roelof19:02:07

what is wierd because this line

(if (s/invalid? page-num) 1 page-num) 
is taking care that page-num is always a num

rads19:02:47

are you sure about that? 🙂

roelof19:02:07

yep, I have checked that before : this line is checking if page is a valid number :

page-num (s/conform ::page page) 

roelof19:02:38

and then here I check if pagenum is a number or invalid :

(if (s/invalid? page-num) 1 page-num) 

roelof19:02:02

if it's invalid I 'change' page-num to 1

roelof19:02:26

I have checked that before on repl

dominicm19:02:49

@roelof you're not changing it (in your example at least)

roelof20:02:21

wierd, on test on repl which I did earlier It seems to work

roelof20:02:57

What I tried to do is when a user uses localhost:3000 the request to the external api is containing the page-number 1

roelof20:02:07

and that looks to happen

roelof20:02:49

I do not get a error message that page-num is not a number

roelof20:02:26

I have made this spec test to take care of it :

(defn ->long [s] (try (Long/parseLong s) (catch Exception _ ::s/invalid)))

(s/def ::page (s/and (s/conformer ->long) (s/int-in 1 471)))

(s/def ::optional-page (s/nilable ::page))

rads20:02:12

how can you make sure the if you pasted above is doing what you expect?

roelof20:02:27

you are right. it's not going to work. I change the test to optional-page because page can be nill or a number and now I see a nullpointer exception

roelof20:02:43

is there a better way to take care that page-num is always 1 or another number even if a user is using a non-valid-number or character

dominicm20:02:11

@roelof It could work without much altering. You just need to use your if in the let, e.g.

[page-num (s/conform page)
 safe-page-num (if (s/invalid? page-num) 0 page-num)
 …]
Then safe-page-num will be 0 if it's invalid

roelof20:02:35

thanks that seems to be working

roelof20:02:48

Now I have this part :

<div class="social-icons">
                            <ul>

                              {% for number in data.pagination-numbers %}

                                <li> number </li>

                              { % endfor %}


                            </ul>
                        </div>

roelof20:02:17

and I see this error :

The template contains orphan tags.. 

roelof20:02:37

data.pagination is coming from the home-route

roelof20:02:38

when I deleting parts. It looks like the for part is not good

roelof20:02:31

is there a way I can send data to the base,html without a route ?

dominicm21:02:23

{ % endfor %} should be {% endfor %}

roelof21:02:54

I have now to make the urls : Is there some tag for the base or can I do

<a href = /page={{number}}  

roelof21:02:34

I want to make a url to the page that has that page-number

roelof21:02:20

I could do < a href = localhost/?page = {{ number}} but then I hardcode the url

roelof21:02:38

and I have to change it when the url is chancing

seancorfield21:02:11

<a href=”?page={{number}}”>text</a> should work

seancorfield21:02:39

That will just add ?page=N to the current URL.

dominicm21:02:06

@seancorfield not additive though. Worth noting, probably not relevant here though

roelof21:02:57

@seancorfield thanks, I will implement that

roelof21:02:09

I almost have the pagination problem solved

roelof21:02:27

and yesterday I changed the layout one more time